Hi All this is my first blog post. I am going to cover viewbinding for beginner coder who is using Jim Wilson course on PluralSight.
You are now ready to implement Jim Wilson code the only difference is how you will reference your views in your layout. If you you look in your res.layout folder you will see two files activity_main.xml and content_main.xml. In your activity_main.xml you will see an include state for content_main.xml you need to give it an id (marked in red).
ViewBinding is Android Studio way of referencing your views in your layout without using findViewById. It has the following advantages over findViewById:
- Type safety
- Null safety
- Compile time error report this means that you will see your errors during compile time instead of runtime
You use the binding variable to reference your views in layout eg Jim Wilson implements his code using koltin default referencing system that you can use with an empty activity. The basic activity that implemnets viewBinging and seems to discard the default kotlin reference system. This just means that you will not be able to reference your views in you layout like Jim Wilson.
val originalValue = textDisplayValue.text.toSting().toInt() val newValue = orginalValue * 2 textDisplayedValue.text = newValue.toString()
You will need to implement the code above by using the viewBinding variable >binding
val originalValue = binding.layout_content_main.textDisplayValue.text.toString().toInt() val newValue = orginalValue * 2 binding.layout_content_main.textDisplayValue.text = newValue.toString()
And this is the basics of using viewBinding to reference you views.