androidkotlinandroid-viewbindingandroid-viewbinder

View binding not working, what did I miss?


I am following the Android Kotlin course on Udacity which suggested to use viewbinding instead of findViewById(). However, I am trying to use it and the text in my application does not update. I've tried to rebuild the app and I have added buildFeatures {viewBinding true} in the build.gradle(:app).

My viewbinding text change is really simple:

val binding = ActivityMainBinding.inflate(layoutInflater)
binding.rollButton.text = "Let's roll"

Is there something I missed about viewbinding?


Solution

  • before using views in the activity you have to inflate view as Content View for the current activity

    val binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)
    binding.rollButton.text = "Let's roll"