Android data binding does not work

I am trying to make a simple test example with Android Data Binding . I want to show the text "test" in an EditText with the name "title" in my snippet, but this text is not displayed. Here is my code:

TestVM.java

 public class TestVM extends BaseObservable { public TestVM() {} @Bindable public String getText() { return "test"; } } 

fr_login.xml

 <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="test" type="de.theappguys.templateandroid.viewmodel.TestVM"/> </data> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginTop="20dp" android:text="@{test.text}" android:textSize="22sp" android:textStyle="bold" android:textColor="@android:color/black" /> </RelativeLayout> </layout> 

Frlogin.java

 @EFragment public class FrLogin extends Fragment { ... @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { FrLoginBinding binding = DataBindingUtil.inflate(inflater, R.layout.fr_login, container, false); return binding.getRoot(); } ... 

build.gradle

 android { ..... dataBinding { enabled = true } .... } 
+5
source share
2 answers

you need to set the value for binding

 FrLoginBinding binding = DataBindingUtil.inflate(inflater, R.layout.fr_login, container, false); binding.setTest(new TestVM()); 

The problem with your code is the lack of communication between your model and Fragment .

+4
source

you need to bind your ViewModel as well. for instance

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { FrLoginBinding binding = DataBindingUtil.inflate(inflater, R.layout.fr_login, container, false); binding.setTest(new TestVM()); return binding.getRoot(); } 
+4
source

Source: https://habr.com/ru/post/1259495/


All Articles