Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The inflate method for my binding is not found (using Android, Data Binding.)

Tags:

I'm using data binding to bind the layouts in my Android app.

I have set up my layout ( my_custom.xml ) and the binding class is generated (MyCustomBinding), but Android Studio does not seem to find the .inflate(...) method of the Binding class right away, marking it as an error ( red text!).

The code seems to be correct though, since it compiles and builds just fine into an APK.

How do I get Android Studio to update correctly ?

Code example:

This is my custom View code:

    public class MyCustomView extends FrameLayout {     public MyCustomView(Context context) {         this(context, null, 0);     }     public MyCustomView(Context context, AttributeSet attrs) {         this(context, attrs, 0);     }     public MyCustomView(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);         MyCustomBinding binding = MyCustomBinding.inflate(inflater, this, true);         binding.aButton.setText("Whatever");     } } 

layout is defined as:

    <?xml version="1.0" encoding="utf-8"?> <layout     xmlns:android="http://schemas.android.com/apk/res/android">     <data>     </data>     <FrameLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         >         <TextView             android:id="@+id/a_button"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="Click me!"             android:padding="10dp"             android:background="#000"             android:textColor="#fff"             android:layout_gravity="center"             />     </FrameLayout> </layout> 

And here's the issue: (highlighted red)

enter image description here

like image 428
treesAreEverywhere Avatar asked Apr 11 '16 14:04

treesAreEverywhere


People also ask

What is data binding and view binding in Android?

The one and only function of View Binding is to bind the views in the code, while Data Binding offers some more options like Binding Expressions, which allows us to write expressions the connect variables to the views in the layout.

What is ActivityMainBinding in Android?

xml so the corresponding generated class is ActivityMainBinding . This class holds all the bindings from the layout properties (for example, the user variable) to the layout's views and knows how to assign values for the binding expressions.


Video Answer


1 Answers

Something is not completing in Android Studio because you haven't actually implemented data binding. Once you add a variable to your layout's data element, the inflate method will be found as you expect. That said, you're really not getting the benefit of databinding by setting the value of the text field directly through the binding. You should instead be setting a View Model in your binding, and then let the binding update the views accordingly. For example:

create a View Model:

public class MyViewModel {     public final ObservableField<String> name;     public MyViewModel(String name) {         this.name = new ObservableField<>(name);     } } 

and use it in your layout

<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android">     <data>         <variable name="model" type="com.victoriaschocolates.conceirge.MyViewModel" />     </data>     <FrameLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         >         <TextView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@{model.name}"             android:padding="10dp"             android:background="#000"             android:textColor="#fff"             android:layout_gravity="center"             />     </FrameLayout> </layout> 

(note the variable declared in the data element, and how it is referenced in the TextView's text attribute)

then bind the two in your custom view:

public class MyCustomView extends FrameLayout {      public MyCustomView(Context context) {         this(context, null, 0);     }      public MyCustomView(Context context, AttributeSet attrs) {         this(context, attrs, 0);     }      public MyCustomView(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);         MyCustomBinding binding = MyCustomBinding.inflate(inflater, this, true);         MyViewModel model = new MyViewModel("Whatever");         binding.setModel(model);     } } 

Of course, it would probably be better still to have the data passed in through a setter in the custom view class, or even passed in from the container view (see http://developer.android.com/tools/data-binding/guide.html#includes)

like image 133
Jeremy Dowdall Avatar answered Oct 02 '22 12:10

Jeremy Dowdall