Data binding from variable in fragment, android?

Viewed 2392

I have a string variable in my fragment which I want to use in XML with data binding This is my fragment

class HomeFragment : Fragment() {

var struc: String = "Organization Structure"

lateinit var binding: FragmentHomeBinding
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    binding = FragmentHomeBinding.inflate(inflater, container, false)
    return binding.getRoot()
}}

This is the XML

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>
    <variable
        name="data"
        type="com.mountmeru.view.HomeFragment" />
</data>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.HomeFragment">
    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tvOrganization"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ivOrganization"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="@{data.struc}"
        android:textAlignment="center"
        android:textColor="@color/mountmerured"
        android:textSize="15sp" />
</FrameLayout></layout>

When I run the application the textview text is never set.

What is wrong here?

2 Answers

First way :

This not direct way which you asked. But you can also do using data class, this is little bit long way but you can add multiple data.

  1. Make data class

    data class Data(var struc: String = "Organization Structure")
    
  2. Add this in xml

    <data>
        <variable
            name="dataModel"
            type="Data" />  // here data class path of step1
    </data> 
    
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".view.HomeFragment">
        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/tvOrganization"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ivOrganization"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:text="@{dataModel.struc}"            // use like this
            android:textAlignment="center"
            android:textColor="@color/mountmerured"
            android:textSize="15sp" />
    </FrameLayout></layout>
    
  3. In Activity or fragment

    private fun setData() {
        val data = Data(struc= "Organization")
        bindObject.dataModel = data
    }
    

Second way :

Or Else direct way is

<data>
    <variable
        name="struc"
        type="String" />  
</data> 

and from activity/fragment

bindObject.struc = "Organization Structure";

Third way :

Or In your case you need to do this in onCreateView and everything works

binding.data = this;

I think you have mistaken while the layout inflating. Please try below code

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
    return binding.getRoot()
}}
Related