How to use View Binding with Included Views?
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.
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.