Reference: vogella
  • Allows to synchronize your user interface with your application model and logic.
  • This minimizes the necessary code.
  1. Layout: “activity_main.xml”:
 <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
    <android.support.constraint.ConstraintLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context="com.example.phuoc.notificationapp.MainActivity">
     <TextView
      android:id="@+id/textViewTitle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello World!"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent" />
 </android.support.constraint.ConstraintLayout>
</layout>
    1. app/build.gradle file
android {
    //...
    //enable dataBinding
    dataBinding {
        enabled = true
    }
}
    1. Activity: “MainActivity”
//activity_main.xml -> ActivityMainBinding
private ActivityMainBinding mMainBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mMainBinding.textViewTitle.setText("OKi");
        mMainBinding.textViewTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //code
            }
        });
    }
Advertisements