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 another Bluetooth device) when I open the button's activity. ie: Without any user input I have to change swipe button's state to enable !

- 7
- 5
2 Answers
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

- 723
- 5
- 6
-
Actually its not a switch its a swipe button . So gotta suggest something else .. – Prashant Rawat Mar 29 '19 at 15:05
-
What class is this ? from external librarie ? I thought that it was a switch because it's a button that you swipe so.. – Kilarn123 Mar 29 '19 at 15:13
-
Actually I used: implementation 'com.ebanx:swipe-button:0.4.0' : In the build gradle... So yes its external library. – Prashant Rawat Mar 29 '19 at 15:17
-
i checked the source code and there is no real setChecked equivalent but there is "toggleState()" which toggle the state. You can check the initial state with isActive(). If this work, i'll update the answer, but write the library name in the question – Kilarn123 Mar 29 '19 at 15:32
-
isActive() only tells us the state of the swipe button , but how can I change its state to enabled ? And there is no toggleState() – Prashant Rawat Mar 29 '19 at 16:04
-
the latest version is 0.8.3, you juste need to change 0.4.0 to 0.8.3 in your gradle file. toggleState() has been added after the version you use – Kilarn123 Mar 29 '19 at 16:09
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:
SwipeButton swipe_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(new OnStateChangeListener() {
@Override
public void onStateChange(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'

- 1,644
- 15
- 31