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! While you're at it, check out some resources Treehouse students have shared here.
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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsChanging 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! :)
Thanks.
import android.app.Activity
import android.os.Bundle
import android.widget.TextView
import android.widget.RelativeLayout
class MealActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_meal)
val foodLabel = findViewById(R.id.foodTextView) as TextView
val drinkLabel = findViewById(R.id.drinkTextView) as TextView
val mealLayout = findViewById(R.id.mealLayout) as RelativeLayout
mealLayout.setBackgroundColor(Color.GREEN);
}
}
1 Answer
Ben Deitch
Treehouse TeacherHey 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
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsThanks Ben, I think I understand now :)
The semi colons are a habit from Java I'm trying very hard to kick lol