1

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
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • http://stackoverflow.com/questions/3739661/android-error-inflating-class – Amit Vaghela Feb 23 '16 at 10:18
  • [documentation about binding](https://developer.android.com/tools/data-binding/guide.html#generated_binding) because you are missing essential parts. Such as `MyLayoutBinding binding = MyLayoutBinding.bind(viewRoot);` – yennsarah Feb 23 '16 at 10:28
  • MyLayoutBinding class is not generated in my case.I am not able to find that binding class . I am not in activity class. Does this matters??? – San Jaisy Feb 23 '16 at 10:34

4 Answers4

6

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
    }
}
Jessica Zeng
  • 324
  • 3
  • 5
0

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

android {
    ....
    dataBinding {
        enabled = true
    }
}
Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
0

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

0

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.
Johny
  • 501
  • 5
  • 16