I am trying to use two-way databinding on a EditText. String values are working fine but I can't get it working for Float values.
I have tried to use a binding adapter that I found in this answer with no luck: Android DataBinding float to TextView
Then I found this Converter class on the android developers website. https://developer.android.com/topic/libraries/data-binding/two-way#kotlin
public class Converter {
@InverseMethod("stringToFloat")
public static String floatToString(float value) {
try {
return String.valueOf(value);
} catch (Exception e) {
return "";
}
}
public static float stringToFloat(String value) {
try {
return Float.parseFloat(value);
} catch (Exception e) {
return 0.0f;
}
}
}
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/width"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Width"
android:inputType="number"
android:text="@={Converter.floatToString(product.width)}"/>
data class Product(
val height: Float?,
val length: Float?,
val width: Float?,
val weight: Float?,
): BaseObservable() {
After using the converter class I get the following error when compiling:
error: cannot generate view binders java.lang.NullPointerException at android.databinding.tool.expr.Expr.lambda$join$0(Expr.java:771)
Your first expression will not work, as there is no reverse expression. Your second expression will not work, as Android will treat it as a string resource. My guess is that you will need to switch your view model to use ObservableField<String> instead of ObservableInt. You can do two-way binding on EditText.
Simple Two Way Data Binding Project Example Let’s start by creating a new Android Studio project. I am naming it as TwoWayDataBinding demo. Step 1 First of all we need to enable data binding in app level build.gradle file. Write this code part inside the android block.
Then, drag and drop an EditText to the bottom of that TextView. User infer constraints button to automatically set constraints. In order to use data binding with this layout, add <layout></layout> tags as the outermost tagsof the layout. After that, add a layout variable of type “User” .
Two-way Data Binding is a technique of binding your objects to your XML layouts so that the layout can send data to your binding object. This is compared to a “traditional” or “one-way” Data Binding setup, where data would only move from your binding object to the layout.
Thanks for the suggestions. The problem was in my model. I had to change the width property from val to var. (val properties cannot be reassigned. These are like final properties in Java)
And instead of using a Converter class I have added a BindingAdapter. Looks more clean for me.
public class TextViewBindingAdapter {
@BindingAdapter("android:text")
public static void setText(TextView view, Float value) {
if (value == null)
return;
view.setText(String.valueOf(value));
}
@InverseBindingAdapter(attribute = "android:text", event = "android:textAttrChanged")
public static Float getTextString(TextView view) {
return Float.valueOf(view.getText().toString());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With