The requirement is to implement a 3 way switch button like this..
[insert switch with no-state]
Almost everywhere I checked, there were only 2-state Switch button and the only 3-way options I found were toggle.
Anyone have used/know how to tackle this situation?
Check if the non-responsive buttons feel sticky or stuck down. If any are sticky or stuck, power off the console and clean the area around the problematic buttons using a soft, dry toothbrush. Avoid using any cleaners or fluids.
For example, a toggle is right control when you want to allow users to turn the Airplane Mode ON or OFF. Toggles are the preferred way to adjust settings on mobile because they take less screen estate (in comparison with two radio buttons).
A latching switch, when wired normally open, can be pressed once to close the electrical circuit and cause something to switch on. When the pressure is removed from a latching switch it remains on until the user switches it off again by pressing it once to re-open the circuit causing a break in the electrical current.
You have to define a class that extends Checkbox
define variables for check state
Override the onClick or onCheck method to change that variable between the 3 states instead of toggling the isChecked variable.
static private final int CHECKED = 1;
private int state;
public CheckBoxTriStates(Context context) {
super(context);
init();
}
public CheckBoxTriStates(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CheckBoxTriStates(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init()
{
state = UNKNOW;
updateBtn();
setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// checkbox status is changed from uncheck to checked.
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
switch (state)
{
case UNKNOW:
state = UNCHECKED;
break;
case UNCHECKED:
state = CHECKED;
break;
case CHECKED:
state = UNKNOW;
break;
}
updateBtn();
}
});
}
private void updateBtn()
{
int btnDrawable = R.drawable.ic_checkbox_indeterminate_black;
switch (state)
{
case UNKNOW:
btnDrawable = R.drawable.ic_checkbox_indeterminate_black;
break;
case UNCHECKED:
btnDrawable = R.drawable.ic_checkbox_unchecked_black;
break;
case CHECKED:
btnDrawable = R.drawable.ic_checkbox_checked_black;
break;
}
setButtonDrawable(btnDrawable);
}
public int getState()
{
return state;
}
public void setState(int state)
{
this.state = state;
updateBtn();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With