San Jaisy
San Jaisy

Reputation: 17088

Data Binding in Android Custom View

I have create a custom layout as show below. I want to perform the databinding with this layout. How can I perform this task.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">
        <TextView
            android:id="@+id/no_internetConnection_Text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_toRightOf="@+id/icon_noInternet_connection"
            android:text="@string/no_Internet_Connection_text" />
    </LinearLayout>
</layout>

Here is the existing code. How can i replace the existing code with databinding code.

public class NetworkConnectionCheck {
    private  Context _context;

    public NetworkConnectionCheck(Context _context) {
        this._context = _context;
    }

    public void CustomToastShow() {
        LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate( R.layout.customtoast, null );
        TextView text = (TextView) view.findViewById(R.id.no_internetConnection_Text);  
        ImageView imageView = (ImageView) view.findViewById(R.id.icon_noInternet_connection);
        Toast toast = new Toast(_context);
        toast.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(view);
        toast.show();
    }
}

Currently I am getting an error. This is because of Layout tag .

Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class layout

Upvotes: 1

Views: 6735

Answers (4)

Johny
Johny

Reputation: 531

For someone else having the same problem, check this:

  • The XML file must have a layout tag
  • The binding class name will be generated based on the XML filename. my_file.xml will result in the name 'myFileBinding'
  • If you don't find the binding class, try to rebuild the project.

Upvotes: 0

Jessica Zeng
Jessica Zeng

Reputation: 324

You need to add

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <merge>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">
        <TextView
            android:id="@+id/no_internetConnection_Text"
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_toRightOf="@+id/icon_noInternet_connection"
        android:text="@string/no_Internet_Connection_text" />
</LinearLayout>
<merge/>
</layout>

And in your custom view

public class NetworkConnectionCheck {
    private  Context context;

    public NetworkConnectionCheck(Context context) {
        super(context);
        init();
    }
    public NetworkConnectionCheck(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public NetworkConnectionCheck(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void CustomToastShow() {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.customtoast, this, true);
        //do what you need to do with the binding
    }
}

Upvotes: 6

Catalin Clabescu
Catalin Clabescu

Reputation: 99

MyLayoutBinding is not generated because your xml file does not contain a data tag. Once you put the data tag inside layout and declare a variable inside data MyLayoutBinding should be generated

Upvotes: 0

Bhavesh Patadiya
Bhavesh Patadiya

Reputation: 25830

I hope you already enable dataBinding in your module gradle file.

android {
    ....
    dataBinding {
        enabled = true
    }
}

Upvotes: 0

Related Questions