Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ToggleButton Text Label Placement for On and Off State

Is there anyway to control the text position for a ToggleButton's on and off state? For instance, I want the text label to be left aligned when on and right aligned when off.

EDIT: Also, I'd to include a little padding for the text on the left and right. About 5dp. and have finer control over the label placement if possible.

ANSWER: This is what I needed!

button.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
button.setPadding(0, 0, 5, 0);
like image 496
user123321 Avatar asked Jul 26 '11 08:07

user123321


2 Answers

  
public class StackActivity extends Activity implements OnCheckedChangeListener {

    private ToggleButton tb;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tb = (ToggleButton)findViewById(R.id.toggleButton1);
        tb.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    {
        if(isChecked)
        {
            tb.setGravity(Gravity.LEFT);
            tb.setPadding(5,0,0,0);    // Set left padding 
        } else
        {
            tb.setGravity(Gravity.RIGHT);
            tb.setPadding(0,0,5,0);    // Set right padding
        }
    }
}
like image 183
Entreco Avatar answered Nov 12 '22 16:11

Entreco


ToggleButton b1 = (ToggleButton) findViewById(R.id.button1);
        if(b1.isChecked()){
            b1.setGravity(Gravity.RIGHT);
        }
        else{
            b1.setGravity(Gravity.LEFT);
        }

Note, that you will not see any changes if the button is not of a minimum size (has to be bigger than the lable text).

like image 29
iDroid Avatar answered Nov 12 '22 16:11

iDroid