0

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?

Marat
  • 1,288
  • 2
  • 11
  • 22
Zertyn
  • 3
  • 1
  • 4

1 Answers1

1

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"
  • It works, but the ```setContentView(binding.root)``` changes the gravity. Is there a way to stop this? – Zertyn Feb 18 '22 at 20:56
  • Edit: I've found a solution for anyone who ever needs help with this too: [link](https://stackoverflow.com/questions/61695769/why-using-view-binding-is-changing-the-layout) – Zertyn Feb 18 '22 at 21:04