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.
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.
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
.R
is 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:
NullPointerException
and ClassCastException
result in no null safety or type safety.#programming #android #kotlin #mobile