1

I need to enable my Button after check some condition, i want to call one method using @InverseBinding OR Two Way data binding and reflect the changes with return value.

my Code :

<Button 
      android:id="@+id/save_btn_disabled_3"
      android:enabled="@={controller}"
      ....
      />

my Two Way data binding logic here :

@InverseBindingAdapter(attribute = "enable")
    fun getEnableButton(view:View, controller:Controller): Boolean {
    //some conditions
        return false
    }

i want to know am i going in correct direction ?, code is ok ? please suggest me.

2
  • two-way data binding use for the attributes available in given link: developer.android.com/topic/libraries/data-binding/… careful not to introduce infinite loops when using two-way data binding. Commented Dec 18, 2019 at 7:46
  • First you check the condition if it is correct then you will enable the button and it onclick method is that it you want? Commented Dec 24, 2019 at 5:10

2 Answers 2

2

First Add Button Status Object

<variable
            name="button"
            type="com.brl.test.app.vm.ButtonStatus" />

<Button 
      android:id="@+id/save_btn_disabled_3"
      bind:state_change="@{button.state}"
      ....
      />

Set your logic in buttonEnabled funtion then Enable Your Button

@BindingAdapter({"bind:state_change"})
  public static void buttonEnabled(TextView view, State state) {
     
    view.setEnabled(true);
  }
Sign up to request clarification or add additional context in comments.

Comments

0

You can set one variable in field like:

<variable
    name="wantsToVisibleProgress"
    type="boolean" />

and set it in your button like this:

<Button
    android:id="@+id/constraint_select_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_button_blue_big"
    android:enabled="@={wantsToVisibleProgress}"
    android:gravity="center"
    android:onClick="@{v -> fragment.onClick()}"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txt_evse_one">

You can set that value like this:

binding.setWantsToVisibleProgress(true);

1 Comment

Thanks for your answer, but i want to call a function, not dependent any variable.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.