How to databind to onTextChanged for an EditText on Android?

Actually it works out of the box. I think my mistake was using an old version of the data binding framework. Using the latest, this is the procedure: View: <EditText android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:id=”@+id/username” android:text=”Enter username:” android:onTextChanged=”@{data.onTextChanged}” /> Model: public void onTextChanged(CharSequence s, int start, int before, int count) { Log.w(“tag”, “onTextChanged ” + s); } … Read more

Set drawable resource ID in android:src for ImageView using data binding in Android

Answer as of Nov 10 2016 Splash’s comment below has highlighted that it is not necessary to use a custom property type (like imageResource), we can instead create multiple methods for android:src like so: public class DataBindingAdapters { @BindingAdapter(“android:src”) public static void setImageUri(ImageView view, String imageUri) { if (imageUri == null) { view.setImageURI(null); } else … Read more

android databinding using “&&” logical operator

&& should be rendered as &amp;&amp;. The official data binding guide has examples of comparison operators where these XML entities are used, for example android:visibility=”@{age &lt; 13 ? View.GONE : View.VISIBLE}” Edit The example expressions I mentioned in the answer disappeared from the English version of the docs since this answer was written. They do … Read more

Kotlin-android: unresolved reference databinding

Try use this configuration: In main build.gradle: buildscript { ext.kotlin_version = ‘<kotlin-version>’ ext.android_plugin_version = ‘2.2.0-alpha4’ dependencies { classpath “com.android.tools.build:gradle:$android_plugin_version” //… rest of the content } } App build.gradle: android { dataBinding { enabled = true } } dependencies { kapt “com.android.databinding:compiler:$android_plugin_version” } kapt { generateStubs = true }

Cannot find symbol DataBindingComponent on Android Studio 3.2 Canary 16 Kotlin project

Databinding libraries are being refactored as a part of androidx refactoring. I found the databinding annotation processor dependency link from google’s maven repository here. I’ve constructed the actual gradle dependency from there. kapt “androidx.databinding:databinding-compiler:3.2.0-alpha16” Update As of Android studio 3.2.0-beta01, databinding no longer needs its annotation processor dependency to be declared in the gradle file, … Read more

How to use Data Binding and Kotlin in Android Studio 3.0.0

UPD: This was fixed for Android Gradle plugin 3.0.0-alpha3, in yout project root build.gradle, change the buildscript dependencies to use classpath ‘com.android.tools.build:gradle:3.0.0-alpha3’ This is actually a bug in the Kotlin Gradle plugin 1.1.2-4 inter-operation with the Android Gradle plugin 3.0.0-alpha1, caused by how the inputs and outputs of the tasks are set (and thus how … Read more

How to use data-binding with Fragment

The data binding implementation must be in the onCreateView method of the fragment, delete any data Binding that exist in your OnCreate method, your onCreateView should look like this: public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { MartianDataBinding binding = DataBindingUtil.inflate( inflater, R.layout.martian_data, container, false); View view = binding.getRoot(); //here data must … Read more

Android Data Binding using include tag

The problem is that the included layout isn’t being thought of as a data-bound layout. To make it act as one, you need to pass a variable: buttons.xml: <layout xmlns:andr…> <data> <variable name=”foo” type=”int”/> </data> <Button android:id=”@+id/button” ….” /> main.xml: <layout xmlns:andr… … <include layout=”@layout/buttons” android:id=”@+id/buttons” app:foo=”@{1}”/> …. Then you can access buttons indirectly through … Read more