androidfindviewbyidandroid-viewbinding

How to use view binding in Android


I have been using findViewById and then ButterKnife to bind views. Recently, I came across this article and am not quite sure how to use it.

I tried doing it but it does not seem to work in Android Studio 3.4.2

val binding = MainActivityBinding.inflate(layoutInflater)
setContentView(binding.root)

then afterwards using these values, as an example :

binding.button....
binding.textView....

Solution

  • ViewBindings are introduced at this moment of this talk at Google IO/19. It will be available with Android Studio 3.6 and as you mentioned, you're using Android Studio 3.4.2 so it's not working. Read the ViewBindings portion in this article for more references.