20

View Binding got released with v3.6.

Docs: https://developer.android.com/topic/libraries/view-binding

My question is, does anyone know how to use view binding with included layouts?

Given Layout that includes another Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/my_header"
        layout="@layout/item_header"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

</LinearLayout>

I am trying to reference items inside of the item_header layout.

binder.my_header (<-- this just returns back the view)
binder.root (<-- this just returns back the root view)

Even if I add an id to the root of the item_header, such as id="@+id/parent_id" and try to reference that, I receive null pointer exceptions

binder.parentId (<-- I have access to views inside of the item_header, however, I receive exceptions. Says that "parentId" cannot be found)

How to reference the layout, item_header?

portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136
  • 2
    In the experiment that I just ran, `binder.my_header` is an `ItemHeaderBinding`, not a `View`. And I can reference widgets inside of that (e.g., `binder.my_header.foo`) without a problem. – CommonsWare Feb 26 '20 at 00:36
  • @CommonsWare You the man! I didn't realize I could reference that. This works like a charm now. Do you mind posting your comment as an answer? I will accept. – portfoliobuilder Feb 26 '20 at 00:46
  • This is a duplicate question. see my answer here https://stackoverflow.com/a/60616894/8040697 – Emad Razavi Apr 16 '20 at 08:27

2 Answers2

19

Let's suppose that the layout in your question is activity_main.xml. The generated view binding class for it is ActivityMainBinding. Similarly, for item_header.xml, the generated view binding is ItemHeaderBinding.

If we pretend that item_header.xml has a TextView named @+id/foo, then you wind up with this chunk of Kotlin:

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val mainBinding = ActivityMainBinding.inflate(layoutInflater)

    setContentView(mainBinding.root)

    mainBinding.myHeader.foo.text = "this is a test"
  }
}

So, the ActivityMainBinding object should have a property with the android:id that you gave to the <include>myHeader in this case. That should be an ItemHeaderBinding, as view binding appears to set up the nested binding object for the <include>. Since myHeader is an ItemHeaderBinding, you can then reference widgets on it just as you would if you directly inflated ItemHeaderBinding yourself.

Note that view binding appears to convert lower_snake_case into lowerCamelCase, so the my_header ID turns into myHeader in terms of the generated code.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
5

I have checked this problem in Java.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/my_header"
        layout="@layout/item_header"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

</LinearLayout>

item_header.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="false"
    android:background="@android:color/transparent">
    <TextView
        android:id="@+id/foo"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());

        setContentView(binding.getRoot());

        binding.myHeader.foo.setText("this is a test");
    }
}}

I checked that it worked in my new project. I hope it will help.

bigant02
  • 176
  • 3
  • 16