Reducing View Boilerplate with ViewBinding

Overview

The View Binding library makes it easy to reduce the need to use findViewById() lookups. Once this option is enabled in your project, special binding classes will be generated from any of your layout XML files. If you have a layout called activity_simple.xml, for instance, a special class called ActivitySimpleBinding will be generated that will have references automatically created for you. Some major advantages are:

  • improved null safety
  • better type safety
  • greater efficiency with complex views

NOTE: the View Binding library is separate from the Applying-Data-Binding-for-Views, which provides two-way and layout variables support.

Setup

Add the following to app/build.gradle file:

android {
  buildFeatures {
        viewBinding true
    }
}

Confirm you have updated to Android Studio v4.0 by going to Android Studio -> About Android Studio.

Once you have enabled this option, make sure to click Rebuild Project. You will need to rebuild your entire project in order to make sure the view binding class are created.

Activity View Lookups

Once you have enabled and recompiled your project, the binding classes should be available to you. In this example, a ActivitySimpleBinding class should have been generated for a activity_simple.xml file. Instead of using setContentView(activity_simple.xml), we replace that line with a call with ActivitySimpleBinding.inflate():

class ExampleActivity extends Activity {
  TextView title;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // activity_simple.xml -> ActivitySimpleBinding
    ActivitySimpleBinding binding = ActivitySimpleBinding.inflate(getLayoutInflater());

    // layout of activity is stored in a special property called root
    View view = binding.getRoot();
    setContentView(view);

    // set bindings more efficiently through bindings
    title = binding.title;       // was title = findViewById(R.id.title)
    title.setText("My title")

    // alternately, access views through binding when needed, instead of variables
    binding.title.setText("My title");

  }
}

Fragment View Lookups

This can be done within Activity, Fragment, or Adapter classes. For example, fragment usage would look like:

public class FancyFragment extends Fragment {
  Button button1;
  Button button2;

  FancyFragmentBinding binding;

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    
    // fancy_fragment.xml -> FancyFragmentBinding
    binding = FancyFragmentBinding.inflate(getLayoutInflater(), container, false);

    // layout of fragment is stored in a special property called root
    View view = binding.getRoot();
    			 
    // TODO Use fields...
    // binding.
    return view;
  }

  @Override public void onDestroyView() {
    super.onDestroyView();
    binding = null;
  }
}

Troubleshooting

  • If you are missing a view binding class even though it is imported, try to Build -> Rebuild Project.

References

Fork me on GitHub