Create an Android Fragment supporting data binding

9/8/2019

Create an Android Fragment supporting data binding

The default Fragment created by Android Studio does not support data binding.

  1. Use VS to create a Fragment (blank).
  2. Replace FrameLayout with layout for data binding, and add data tag accordingly.
  3. Replace the auto-created TextView with android.support.constraint.ConstraintLayout.
  4. Add views to android.support.constraint.ConstraintLayout, and do data binding if needed.
  5. Modify onCreateView: a. change inflater.inflate() to DataBindingUtil.inflate(); b. set setOnClickListener for each button to this (i.e. the Fragment) and implement click() accordingly.  Note swith(v.getId()) cannot be used for Fragment.  One must use if...else if... instead to handle calling from different buttons.
  6. Modify newInstance() to pass necessary parameters to instantiate the Fragment.