View Binding— Layout and Binding Expression

<?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>

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);

}
}
ActivityWelcomBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_welcom);
viewDataBinding.setWelcome(welcomMessage);
<variable
name="welcome"
type="com.example.view_binding.model.WelcomMessage"
/>

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

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

Suraj Vishwakarma

17 Followers

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