Get started with Android View Binding

April 12, 2020

Image from instagram

View Binding is one of the many new architecture components introduced by android for a better and faster development experience. In simple terms, it lets you use your views defined in your xml files in your activities/fragments without creating many objects of many different view types.

Let’s take an example xml layout: activity_main.xml

<LinearLayout
    android:id="@+id/titles"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="vertical">

        <ImageView
            android:id="@+id/image_titles"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:srcCompat="@drawable/titles_unselected"
            tools:ignore="VectorDrawableCompat" />

        <TextView
            android:id="@+id/text_titles"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="3dp"
            android:text="Titles" />
 </LinearLayout>

Normally, if we have to use a view in our activity we do something like this:

public class MainActivity extends AppCompatActivity{

    LinearLayout titles;
    ImageView imageTitles;
    TextView textTitels;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        titles = findViewById(R.id.titles);
        textTitels = findViewById(R.id.text_titles);
        imageTitles = findViewById(R.id.image_titles);
    }

Creating objects of specific view types and then initializing them using findViewById() method. This is time consuming and makes up a lot of the boilerplate code.

View Binding is the solution we all have been looking for a while. With ViewBinding, you don’t have to create new objects for different view types of initialize them for that matter. All of this will be done for you. Let’s see how.

  • First step is enabling view binding. For this, put the following line in your app’s build.gradle file:
    android {
        ...
        viewBinding {
            enabled = true
        }
        ...
    }

Note: View Binding is available in Android Studio 3.6

That’s actually it!

  • Now in your Activity:

    public class MainActivity extends BaseActivity{
    
    private ActivityMainBinding binding;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
       binding = ActivityMainBinding.inflate(getLayoutInflater());
       View view = binding.getRoot();
       setContentView(view);
    
        binding.titles.setOnClickListener(...); //The LinearLayout view
        binding.textTitles.setText("New Titles title") // TextView view
        binding.imageTitles.setImageDrawable(...) // ImageView view
    }
    
    ...
    }

The respective id you give in your xml view will become the member name by which you can access your views from your layouts.

  • For using View Binding in fragments:

    public class MyFragment extends BaseFragment{
    
    private MyFragmentBinding binding;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       binding = MyFragmentBinding.inflate(inflater, container, false);
       View view = binding.getRoot();
    
        binding.titles.setOnClickListener(...); //The LinearLayout view
        binding.textTitles.setText("New Titles title") // TextView view
        binding.imageTitles.setImageDrawable(...) // ImageView view
    
        return view;
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
    
    ...
    }

Written by Gagandeep Rangi who likes to talk about himself in third person. Twitter Instagram

Email icon