Skip to content Skip to sidebar Skip to footer

How To Change State Of Swipe Button From Code?

I am using a swipe button from com.ebanx:swipe-button library in my application and I wish to change the state of the swipe button to enable (based on the information recieved via

Solution 1:

You can use toggleState()

SwipeButton mSwipeButton; = findViewById(R.id.my_swipe_button);
mSwipeButton.toggleState();

if you use an older version where toggleState is not available, use collapseButton(); or expandButton(); to collapse or expand the swipe button

Solution 2:

There are two issues with the library you're using, first is coding bug, second is wrong documentation, but that's not the case.

to make the button active:

SwipeButtonswipe_btn= findViewById(R.id.swipe_btn);
swipe_btn.setEnabled(true);

now by default, the button state is closed and you can change that in the xml file i.e the layout where you created the button, you will see something like below:

<com.ebanx.swipebtn.SwipeButton
        app:initial_state="disable" //change to enable will make button open by default
        app:has_activate_state="true"
        />

Finally to monitor the state of the button, you will have to listen to the state changes like below:

swipe_btn.setOnStateChangeListener(newOnStateChangeListener() {
            @OverridepublicvoidonStateChange(boolean active) {
                Toast.makeText(MainActivity.this, "IS "+active, Toast.LENGTH_LONG).show();
            }
        });

if active, the button is open, else it's close.

Note: When I say open, I mean the button is toggledOn, and when I say close, it means the other way round(toggleOff).

The bug here is that when you use swipe_btn.toggleState(); The button will be deactivated, meaning it will not even respond to click event which is not right, so the way around is to use the onStateChangeListener as I use it above so that when the button is open you can do something and when it's close you can still do anything.

Note: library version: 'com.ebanx:swipe-button:0.8.3'

Post a Comment for "How To Change State Of Swipe Button From Code?"