Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App with Kotlin Improving Our Code Dynamically Changing the Background Color

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,250 Points

Changing background colour of a layout dynamically

I'm a bit confused here.

How am I supposed to change the colour of the required layout without an Event Listener. But then, I attach that to a Button, don't I? I don't see one, so do I create one? or is there a bug to the challenge! :)


import android.app.Activity
import android.os.Bundle
import android.widget.TextView
import android.widget.RelativeLayout

class MealActivity : Activity() {

  override fun onCreate(savedInstanceState: Bundle) {

    val foodLabel = findViewById(R.id.foodTextView) as TextView
    val drinkLabel = findViewById(R.id.drinkTextView) as TextView
    val mealLayout = findViewById(R.id.mealLayout) as RelativeLayout


1 Answer

Ben Deitch
Ben Deitch
Treehouse Teacher

Hey Jonathan!

While that does work, with Kotlin we don't need to use the 'setXXXXX' methods; we can just set the properties directly: mealLayout.backgroundColor = Color.GREEN

Also, get rid of that semicolon :)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,250 Points

Thanks Ben, I think I understand now :)

The semi colons are a habit from Java I'm trying very hard to kick lol