Reputation: 17088
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
Reputation: 531
For someone else having the same problem, check this:
Upvotes: 0
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
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
Reputation: 25830
I hope you already enable dataBinding in your module gradle file.
android {
....
dataBinding {
enabled = true
}
}
Upvotes: 0