I can't get layouts with merge root tag to work with view binding. Is it possible?
include_layout.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">
<include
android:id="@+id/include_test"
layout="@layout/merge_layout"/>
</FrameLayout>
merge_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/include_test">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A Title"/>
</merge>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = IncludeLayoutBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.includeTest.title.text = "New Title"
}
Runtime exception:
java.lang.NullPointerException: Missing required view with ID: includeTest
View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.
In Android, for reusing the complete layouts we can use the <include/> and <merge/> tags to embed another layout inside the current layout. Android offers a variety of widgets to provide small and reusable interactive elements. In some cases we might also need to reuse larger components that require a special layout.
Based on this article it's possible
for example :
my_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/my_include_layout"/>
</LinearLayout>
my_include_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</merge>
MyFragment.kt
class MyFragment: Fragment() {
private val binding: MyFragmentBinding get() = _binding!!
private var _binding: MyFragmentBinding? = null
private val myIncludeLayoutBinding: MyIncludeLayoutBinding get() = _myIncludeLayoutBinding!!
private var _myIncludeLayoutBinding: MyIncludeLayoutBinding? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = MyFragmentBinding.inflate(inflater, container, false)
_myIncludeLayoutBinding = MyIncludeLayoutBinding.bind(binding.root)
return binding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
myIncludeLayoutBinding.button.setOnClickListener{
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
_myIncludeLayoutBinding = null
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With