The Android development environment has been based on three types of files since the beginning: XML, Kotlin, and Java. XML files contain everything related to design, and Kotlin/Java files include anything other than the design part.

Eventually, UI and business logic needed to be linked, and that’s what this article is all about. Let’s see how finding a view in the layout is evolved over the years.


‘findViewByID’

findViewByID is the first one in the line, and it was introduced in API Level 1 by the Android team. This function provides a view object as a return type and developers link this object to the view created in the View class.

Every view in Android is extended from the View class. Thus, all the views used in the XML layout, one way or another, extends to the View class. findViewByID returns an instance of that class.

class ExampleActivity extends Activity {

	  TextView title;
	  TextView subtitle;

	  @Override public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.simple_activity);

	    title = (TextView) findViewById(R.id.title)
	    subtitle = (TextView) findViewById(R.id.subtitle)

	  }
	}

Usage of ‘findViewByID’

Recently, the Android team made an enhancement to this function to overcome view casting. So the developers no longer need to cast the views while linking with findViewByID.

Despite the efforts to enhance findViewByID, many developers felt it wasn’t the best approach. So people started creating alternatives like Butter Knife, and at some point, the Android team came up with data binding (though its primary focus isn’t only to link the views).


Butter Knife

The Android Butter Knife library is a lightweight view0injection library that works on annotations. It was one of the first libraries that seemed to be a successful alternative to the traditional findViewByID.

It was created by Jake Wharton, and you can find the documentation on how to use Butter Knife on its official website. This library links the views from a layout using the@BindView annotation. For a while, developers used it as a sort of formal alternative to findViewByID. Let’s take a look at how to use it:

class ExampleActivity extends Activity {

	  @BindView(R.id.title) TextView title;
	  @BindView(R.id.subtitle) TextView subtitle;

	  @Override public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.simple_activity);
	    ButterKnife.bind(this);

	  }
	}

Butter Knife usage

It’s almost similar to findViewByID; the only benefit for developers in terms of removing boilerplate code is that we don’t need to implement the declaration and initialization of the views separately.


Data Binding

The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format, rather than programmatically.

The Data Binding Library generates binding classes for the layouts, which we can use in Android components like Activity and Fragment. Data binding is a sort of declarative solution with type and null safety.

To make a layout support data binding, we have to wrap the content of the file with the layout tag. This will generate a binding file of that layout with a reference to all of the views in it. Have a look:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
	        xmlns:app="http://schemas.android.com/apk/res-auto">

	    <ConstraintLayout... /> <!-- UI layout's root element -->

	</layout>

Wrapping the root content inside a ‘layout’ tag

Once we’ve finished that part in the XML, a binding class will be generated, and we need to use it in the View class. This binding-file instance contains all of the views in the layout. Have a look:

override fun onCreate(savedInstanceState: Bundle?) {
	    super.onCreate(savedInstanceState)

	    val binding: ActivityMainBinding = DataBindingUtil.setContentView(
	            this, R.layout.activity_main)

	    binding.tvName.text = "John"
	}

#software-engineering #kotlin #mobile #androiddev #programming #android

The Evolution of View Linking in Android
1.35 GEEK