How to use view binding in Android

View binding is only available in Android Studio 3.6 Canary 11+. Firstly, you upgrade Android studio with android gradle plugin version 3.6.0-alpha11+ (you can use beta both of them for now, stable version not yet released but you can use beta) then add below code in build.gradle

android {
        viewBinding.enabled = true
}

Now you can use like we used with data binding like that:

private lateinit var binding: ActivityMainBinding

@Override
fun onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    binding.textView.text = "Hello World"
}

that's it.


There is a couple of things you should do and I try to make it organized and listed: (Based on Android Developers docs from this link and my personal experiences)

  1. You need to use Android Studio 3.6 canary11+ (I'm currently using Android Studio 4 and it is doing the job well for me)

    You can find it from here: https://developer.android.com/studio/archive

  2. You need to upgrade your Gradle wrapper to Gradle "5.6.4" and Gradle build tool to "3.6.0-rc01", higher versions also work so don't be afraid to be updated

    distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
    dependencies {

        ...
        classpath 'com.android.tools.build:gradle:3.6.0-rc01'

    }
  1. To enable view binding in a module, add the viewBinding element to its build.gradle file, as shown in the following example:
    android {
    ...
      viewBinding {
        enabled = true
      }
    }
  1. If you want a layout file to be ignored while generating binding classes, add the tools:viewBindingIgnore="true" attribute to the root view of that layout file:
    <LinearLayout
        ...
        tools:viewBindingIgnore="true" >
        ...
    </LinearLayout>
  1. If view binding is enabled for a module, a binding class is generated for each XML layout file that the module contains. Each binding class contains references to the root view and all views that have an ID. The name of the binding class is generated by converting the name of the XML file to camel case and adding the word "Binding" to the end.

    For example, given a layout file called result_profile.xml:

    <LinearLayout ... >
        <TextView android:id="@+id/name" />
        <ImageView android:cropToPadding="true" />
        <Button android:id="@+id/button"
            android:background="@drawable/rounded_button" />
    </LinearLayout>

The generated binding class is called ResultProfileBinding. This class has two fields: a TextView called name and a Button called button. The ImageView in the layout has no ID, so there is no reference to it in the binding class.

Every binding class also includes a getRoot() method, providing a direct reference for the root view of the corresponding layout file. In this example, the getRoot() method in the ResultProfileBinding class returns the LinearLayout root view.

  1. To set up an instance of the binding class for use with an activity, fragment or card view adapter perform the following steps:
  • in the activity's onCreate() method:
    private ResultProfileBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ResultProfileBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    setContentView(view);
}
  • in the fragments's onCreateView() method:
    private FragmentHousesBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentHousesBinding.inflate(inflater, container, false);

        init();

        return binding.getRoot();
    }
  • in the card view adapter's onCreateViewHolder() method:
    HouseCardPropertyFragmnetBinding binding;

    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        binding = HouseCardPropertyFragmnetBinding.inflate(LayoutInflater
            .from(parent.getContext()), parent, false);

        return new Holder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull HouseAdapter.Holder holder, int position) {
        holder.bindData(getItem(position));
    }

    class Holder extends RecyclerView.ViewHolder {

        HouseCardPropertyFragmnetBinding view;

        Holder(@NonNull HouseCardPropertyFragmnetBinding v) {
            super(v.getRoot());
            view = v;
        }

        void bindData(Tag item) {
            view.tagTxt.setText(item.Name);

        }
    }

that's it you are free from the findViewById from now on ;)


ViewBinding is only available from Android Studio 3.6 and above

1:- You need to upgrade your gradle build toold to 3.6.1 in build.gradle (Project level)

 dependencies {
        classpath 'com.android.tools.build:gradle:3.6.1'
             }

2:- You need to enable viewBinding in build.gradle(app)

 android {
viewBinding {
    enabled = true
}}

Once view binding is enabled a binding class is generated for each XML layout. The name of the binding class is generated by converting the name of the XML file to camel case and adding the word "Binding" to the end.

Example:- if a layout file is named as "add_item_activity.xml" then the Name of Binding class will be "AddItemActivityBinding"

3:-To set up an instance of the binding class for use with an activity, create a instance of Binding class, here we will create instance of "AddItemActivityBinding" and will call the static inflate method generated in the binding class

Get a reference to the root view by calling the getRoot() method and pass this root view in setContentView() method

public class AddItemActivity extends AppCompatActivity {
    private AddItemActivityBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = AddItemActivityBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

        //now we can access views by their IDs 
        binding.tvTitleMyOrder.setText("Title goes here");

    }

}

Now we can access views by their IDs using instance of binding class