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.

Luthervd
  • 1,388
  • 2
  • 14
  • 25
  • can you explain why you need textinput at first click and numberpad in second click? – Linh Dec 15 '16 at 00:58
  • If you read the question you will see that is not the required but the actual behaviour. – Luthervd Dec 15 '16 at 01:10
  • Hi, roundInEdit is what type of variable – Karthik Sridharan Dec 19 '16 at 13:17
  • Hi it's a float – Luthervd Dec 19 '16 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 Dec 20 '16 at 09:27
  • It's using the built in two way binding so there is no custom binding. I would think a float would always need to be converted to text when bound to an edit text? – Luthervd Dec 20 '16 at 09:29
  • You wrote "have written my own Binding and InverseBinding adapter" - sorry for my wording. Unfortunately, I think you have to create a custom `InverseAdapter` to enable the two-way Binding, because the output from an `EditText` is a `string`. (Which can't be automagically converted to a `float`) – yennsarah Dec 20 '16 at 09:34
  • Hi sorry see what you mean. The two way binding does work with conversion so the built in way converts to a string under the hood. I think it works with all primitives out of the box. I wrote my own to see if the behaviour would differ and it does not so reverted back to built in. Guess I'll have to try again with my own adapter but was getting exactly the same behaviour with text keyboard popping up on first click and then number pad on second. – Luthervd Dec 20 '16 at 09:58

2 Answers2

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;
        }
    }
}
Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60