ToggleButton Text Label Placement for On and Off State
Asked Answered
K

5

6

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);
Kiger answered 26/7, 2011 at 8:46 Comment(2)
Hi. Can you please provide the layout xml file part where you declared the toggle button? Thank you.Danieldaniela
I've tried this and actually I thought the text will be placed on the right side of the green line, but instead is placed on the right side above the green line. Seems that placing the text on the right side of the green line is not possible - reference: #11085456Danieldaniela
P
7
  
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
        }
    }
}
Papyrus answered 26/7, 2011 at 9:0 Comment(3)
Thank you! Is there a way to have finer grain over the position of the text as well? Like LEFT but 5dp padding?Kiger
I've added options for setting the padding on the togglebutton. It's really as simple as toggleButton.setPaddin(int left, int top, int right, int bottom); For more control you could consider setting the LayoutParams for the ToggleButtonPapyrus
Yup, thanks! I just figured that out too!. You rock. Thanks man!Kiger
S
2
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).

Severance answered 26/7, 2011 at 8:54 Comment(1)
Thank you! Is there a way to have finer grain over the position of the text as well? Like LEFT but 5dp padding?Kiger
W
1

Since ToggleButton is a subclass of TextView, try to use android:gravity="left".

Please prefer to http://developer.android.com/reference/android/widget/TextView.html#attr_android:gravity.

Wonderment answered 26/7, 2011 at 8:52 Comment(0)
S
1

Change the alignment by changing the gravity whenever the button is clicked by adding some code in the OnClickListener like this:

toggleButton.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) {
               if (((ToggleButton)v).isChecked())
                    toggleButton.setGravity(Gravity.LEFT);
               else
                   toggleButton.setGravity(Gravity.RIGHT);
            }
        });
Sclerotomy answered 26/7, 2011 at 9:8 Comment(3)
Thank you! Is there a way to have finer grain over the position of the text as well? Like LEFT but 5dp padding?Kiger
I do not know any good simple way to get finer grain over the position. But I do know one ugly solution, it would be to add a blank space at the beginning of left aligned text and then another blank space at the end of the right aligned text. Like this " On" and "Off "Sclerotomy
Yeah, I was considering that too, but I also need padding on the TOP! Seems like a toggle buttons doesn't allow for a lot of customizations.Kiger
K
1

This is what I needed!

button.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
button.setPadding(0, 0, 5, 0);
Kiger answered 28/7, 2011 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.