Skip to content Skip to sidebar Skip to footer

Layout Binding Integer Value Limit From 0 To 10 In Android With Databinding

I am trying to set the limit of an integer with a minimum value of 0 and maximum of 10 in android with the help of databinding. For that i have a bindable adapter which set the val

Solution 1:

I think it would be easier if you handle the clicks a little differently. The lambda expression part only works in Android Studio 2.1 and above.

<Buttonandroid:onClick="@{(view)->Handlers.increment(view, 10)}".../><Buttonandroid:onClick="@{(view)->Handlers.decrement(view, 0)}".../><TextViewapp:quantity="@{quantity}"/>

And then your handler class has:

publicstaticvoidincrement(View view, int max) {
    FragmentBindingbinding= DataBindingUtil.findBinding(view);
    binding.setQuantity(Math.max(max, binding.getQuantity() + 1));
}

publicstaticvoiddecrement(View view, int min) {
    FragmentBindingbinding= DataBindingUtil.findBinding(view);
    binding.setQuantity(Math.min(min, binding.getQuantity() - 1));
}

Alternatively, you can use full-blown two-way binding. In the forthcoming Android Studio 2.2, you'll be able to do this:

<Buttonandroid:onClick="@{()->Handlers.increment(quantityView, 10)}".../><Buttonandroid:onClick="@{()->Handlers.decrement(quantityView, 0)}".../><TextViewandroid:id="@+id/quantityView"app:quantity="@={`` + quantity}"/>

And then your handler class has:

privatestatic int getCurrentIntValue(TextView view) {
    try {
        returnInteger.parseInt(view.getText().toString());
    } catch (NumberFormatException e) {
        return0;
    }
}
publicstaticvoidincrement(TextView view, int max) {
    int value = getCurrentIntValue(view);
    binding.setQuantity(Math.max(max, value + 1));
}

publicstaticvoiddecrement(View view, int min) {
    int value = getCurrentIntValue(view);
    binding.setQuantity(Math.min(min, value - 1));
}

The trick added in Android Studio 2.2 is the support for the conversions for string concatenation with empty string. It is a useful shortcut. Without that (Android Studio 2.1), you'll need to add your own two-way binding for integer-to-TextView text:

@InverseBindingAdapter(attribute = "quantity")publicstaticintgetQuantity(TextView view) {
    return getCurrentIntValue(view);
}

and here's a simplified binding adapter. Use the one in TextViewBindingAdapter.java as a template if you need to add text watchers to the same TextView:

@BindingAdapter("onQuantityChanged")publicstaticvoidsetQuanityWatcher(TextView view,
        final InverseBindingListener quantityChanged) {
    TextWatcher newTextWatcher;
    if (quantityChanged == null) {
        newTextWatcher = null;
    } else {
        newTextWatcher = newTextWatcher() {
            @OverridepublicvoidafterTextChanged(Editable s) {
                quantityChanged.onChange();
            }
            // others are empty...
        }
    }
    TextWatcheroldTextWatcher= ListenerUtil.trackListener(
        view, newTextWatcher, R.id.textWatcher);
    if (oldTextWatcher != null) {
        view.removeTextChangeListener(oldTextWatcher);
    }
    if (newTextWatcher != null) {
        view.addTextChangedListener(newTextWatcher);
    }
 }

Note that I haven't compiled any of this, so there may be typos.

Post a Comment for "Layout Binding Integer Value Limit From 0 To 10 In Android With Databinding"