Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android two-way databinding Float to EditText

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)
like image 574
Danny Spijker Avatar asked Aug 19 '19 10:08

Danny Spijker


People also ask

Is it possible to do two-way binding on edittext in Android?

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.

How to enable two way data binding in Android Studio?

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.

How to use data binding with a textview in Android?

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” .

What is two-way data binding in Java?

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.


1 Answers

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());
    }
}
like image 70
Danny Spijker Avatar answered Sep 22 '22 06:09

Danny Spijker