View Binding— Layout and Binding Expression

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.

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.

Binding data

        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.

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

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

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

--

--

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suraj Vishwakarma

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