View Binding— Layout and Binding Expression

Suraj Vishwakarma
2 min readOct 13, 2019

In previous we have seen the basic implementation and advantages of using view binding here we will be working on real example in case miss the previous section click on this View Binding -Overview link.

Layout and Binding Expression

It allows you to write expressions that handle events dispatched by the views.Let’s create and R.layout.activity_welcom. Layout binding (Data binding) layout files are slightly different and start with a root tag of layout followed by a data element and a view root element.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

<variable
name="welcome"
type="com.example.view_binding.model.WelcomMessage"
/>

</data>


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>

<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:text="@{welcome.welcomeMessage}"
></TextView>

<TextView
android:id="@+id/version"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_weight="1"
android:padding="10dp"
android:text="@{welcome.version}"
></TextView>
</RelativeLayout>
</layout>

Variable in data it is nothing but describe the property which we are going to use in the layout for example @{welcome.version}, @{welcome.welcomeMessage} here “@{}” is nothing but the attribute properties.

Data object

Let us create POJO(Plain Old Java Object) class named as WelcomMessage. This class has the object which is never going to change. It is common in applications to have data that is read once and never changes thereafter.

public class WelcomMessage {
String welcomeMessage, version;

public WelcomMessage(String welcomeMessage, String version) {
this.welcomeMessage = welcomeMessage;
this.version = version;
}

public String getWelcomeMessage() {
return welcomeMessage;
}

public String getVersion() {
return version;
}
}

Binding data

public class WelcomActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

WelcomMessage welcomMessage = new WelcomMessage("Name : Welcome to Data Binding", "Version :2.0.1");
ActivityWelcomBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_welcom);
viewDataBinding.setWelcome(welcomMessage);

}
}

Here “ActivityWelcomBinding” it is a binding class which will automatically get create with the suffix Binding.

ActivityWelcomBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_welcom);

Once Binding class is created let us add the data into.

viewDataBinding.setWelcome(welcomMessage);

Here “setWelcome” setter value will automatically got created when we have add the below variable into our layout file.

<variable
name="welcome"
type="com.example.view_binding.model.WelcomMessage"
/>

Now you have successfully completed Layout Binding expression, Let us jump to View Binding Event Handling.

--

--

Suraj Vishwakarma

Mobile Application Developer | Flutter Developer | Project Manager | Team Leader | Scrum Certified | PMI APC Appeard | Writing (starting soon)