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 trial

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

Enyang Mercy
PLUS
Enyang Mercy
Courses Plus Student 2,339 Points

Android(Kotlin) set backgroundColor for relativeLayout

Help!!!!!!!!!!!!! ERROR: Bummer! MealActivity.kt:14:5: error: unresolved reference: relativeLayout relativeLayout!!.setBackgroundColor(Color.GREEN) I dunno what is wrong with my line of code. Pls help with possible answers

MealActivity.kt
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
    relativeLayout!!.setBackgroundColor(Color.GREEN)
  }
}

2 Answers

Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

Hi Enyang Mercy,

For some reason it looks like you've deleted part of the challenge code

val mealLayout = findViewById(R.id.mealLayout) as RelativeLayout

So either tack that back in or restart the challenge to get it back. Moving on, the relative layout is the mealLayout, so just set the background on that after you add it back in.

mealLayout.setBackgroundColor(Color.GREEN)

Best of luck, and happy coding, :dizzy:
Dave