Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3 state switch button? [closed]

The requirement is to implement a 3 way switch button like this..

switch

yes

[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?

like image 484
Zx5 Avatar asked May 31 '17 08:05

Zx5


People also ask

Why switch button is not working?

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.

Is toggle right on or off?

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).

How does a latching push button switch work?

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.


1 Answers

  • 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();
}

} 
like image 167
Ege Kuzubasioglu Avatar answered Oct 13 '22 18:10

Ege Kuzubasioglu