Since the start of Android development, there have been many ways to access views from XML layout files inside the classes to perform operations on them. Let’s go over the evolution of importing views.

findViewById

Initially, we used the findViewById() method by passing the view IDto access the view. This added a lot of boilerplate code in all the class files that were inflating layouts. Writing these lines of code repeatedly just to find the views was neither interesting nor comfortable.

Syntax

TextView emailTextView;
emailTextView = (TextView) findViewById(R.id.txt_email);
  • R.id.txt_email means a view that is defined in any layout having an ID name of txt_email.
  • Ris a class in Android that has the set of all the view IDs.
  • findViewById is a method that is used to find the view from the layout resource file that is attached to the current activity.

The drawbacks are:

  1. NullPointerException and ClassCastException result in no null safety or type safety.
  2. More boilerplate code.

#programming #android #kotlin #mobile

Replace Deprecated Kotlin Synthetics With Jetpack View Binding in Android
1.60 GEEK