Khemraj Sharma
Khemraj Sharma

Reputation: 58964

Change an Int value on button click databinding

I can do this programatically, but i want to know best approach using android databinding.

Here is my xml layout. I want to increase or decrease int value onClick of some button. I don't want handle click in activity class. Is this possible, if yes.

In below layout you can see an Integer value count. Value count should be changed on click of Button.

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <import type="android.view.View"/>

        <variable
            name="activity"
            type="com.amelio.ui.activities.ActivityProductDetail"/>

        <variable
            name="count"
            type="Integer"/>
    </data>

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        >

        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:onClick="@{() -> count++}"
            android:text="Less"
            />

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@{`` + count}"
            />

        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/ivBtnPlus"
            android:onClick="@{() -> count--}"
            android:text="Add"
            />
    </LinearLayout>
</layout>

Upvotes: 1

Views: 3426

Answers (1)

aaroncio
aaroncio

Reputation: 307

It will depend on what you are trying to achieve, if you want to make your code more testable, I'd suggest that you handle the click in the XML which will call a function in your Activity or other class where you can test the behavior of this function and if you need to add more complex logic like data validation, database storage, etc, it will be better. In the end the view should be dummy and it allows you to reuse the code as in the case that other function handle it.

Also having a bunch of logic in the XML is a bad practice which will make your code unmaintainable. I'd suggest you to read about the MVVM pattern and data binding Android MVVM Design Pattern

Try this:

<Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="@{() -> activity.myCountMethod()}"
        android:text="Less"
        />

In Activity you must implement myCountMethod(), if you don't want to have in your activity you can add another variable with the class that you want like:

<data>

    <import type="android.view.View"/>

    <variable
        name="activity"
        type="com.amelio.ui.activities.ActivityProductDetail"/>
    <-- Add OtherClass -->
    <variable
        name="otherClass"
        type="com.amelio.ui.activities.OtherClass"/>

    <variable
        name="count"
        type="Integer"/>
</data>


<Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="@{() -> otherClass.myCountMethod()}"
        android:text="Less"
        />

You can find more info here:

https://developer.android.com/topic/libraries/data-binding/expressions#listener_bindings

Upvotes: 1

Related Questions