9

I am trying to bind:

 @Bindable
public float getRoundInEditAmount()
{
    return roundInEdit.getAmount();
}

@Bindable
public void setRoundInEditAmount(float amount)
{
    roundInEdit.setAmount(amount);
    notifyPropertyChanged(BR.roundInEditAmount);
}

to

 <EditText
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:inputType="numberDecimal"
            android:text="@={`` + weightSet.roundInEditAmount}"
            ></EditText>

However on clicking the EditText I am presented with a a text input not the number pad. If I click this EditText again I am then presented with the number pad. If the field has been defaulted to 50.0 or another value I cannot delete these amounts. I can enter text though and it does persist.

Has anyone else come across this behavior with the text input coming up on first click rather than the number pad? Also does the two way binding on EditText work the way I am expecting. I have written my own Binding and InverseBinding adapter and they behave in the same way -> TextInput on first click and then number pad on second click but you cant delete the number that you start with.

8
  • can you explain why you need textinput at first click and numberpad in second click?
    – Linh
    Commented Dec 15, 2016 at 0:58
  • If you read the question you will see that is not the required but the actual behaviour.
    – Luthervd
    Commented Dec 15, 2016 at 1:10
  • Hi, roundInEdit is what type of variable Commented Dec 19, 2016 at 13:17
  • Hi it's a float
    – Luthervd
    Commented Dec 19, 2016 at 15:15
  • Can you please post your custom BindingAdapters? You convert your float to a string in your initial binding, which causes the problem with the keyboard.
    – yennsarah
    Commented Dec 20, 2016 at 9:27

2 Answers 2

17

try like this

<EditText
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:inputType="numberDecimal"
            android:text="@={String.valueOf(weightSet.roundInEditAmount)}"/>
14

If you use Android Databinding Library, it solves by creating binding adapter.

public class BindingUtils {

    @BindingAdapter("android:text")
    public static void setFloat(TextView view, float value) {
        if (Float.isNaN(value)) view.setText("");
        else view.setText( ... you custom formatting );
    }

    @InverseBindingAdapter(attribute = "android:text")
    public static float getFloat(TextView view) {
        String num = view.getText().toString();
        if(num.isEmpty()) return 0.0F;
        try {
           return Float.parseFloat(num);
        } catch (NumberFormatException e) {
           return 0.0F;
        }
    }
}
5
  • 1
    How do you use this with xml. When use android:text="@={BindingUtils.setFloat(marker.latitude)}" with EditText i get cannot find method setFloat(java.lang.String) binding error
    – Thracian
    Commented Jul 2, 2018 at 10:31
  • 1
    @Thracian It's automatically used when you assign a value of type float to a android:text XML property.
    – toXel
    Commented Sep 5, 2018 at 15:11
  • My problem is, that it says: Cannot find a getter that accepts type androidx.livecycle.MutableLiveData<java.lang.Float>
    – Andrew
    Commented Aug 30, 2020 at 18:39
  • @Andrew can you post some code examples or add new question for it? Commented Aug 31, 2020 at 19:22
  • @XIII-th Never mind, it was a problem on my side, sorry.
    – Andrew
    Commented Sep 1, 2020 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.