Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use <merge> with view binding? [duplicate]

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

like image 256
ham Avatar asked Jan 15 '20 09:01

ham


People also ask

Can I use both data binding and view binding?

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.

How to merge two layouts in android?

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.


1 Answers

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
 }
}
like image 70
Eko Yulianto Avatar answered Oct 20 '22 17:10

Eko Yulianto