ToogleButton aligns the text label to the right

I want to align the text in ToogleButton right of this panel, which turned green / white when clicked.

To be more explicit, I will post the code, as well as the result that I have.

  <ToggleButton android:id="@+id/ToggleButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" layout_gravity="center_vertical" android:textOff="Off" android:textOn="On"/> ToggleButton onOffButton = (ToggleButton) findViewById(R.id.ToggleButton01); onOffButton.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL); onOffButton.setPadding(0, 0, 5, 0); 

and the result: enter image description here

I want the panel and text to not overlap.

0
source share
2 answers

Have you tried athletic gravity?

 android:gravity="center" 

the difference between android:layout_gravity="center" and android:gravity="center" is that the first will center your View inside your layout , and the second will center the contents of this View .

This is my layout of your button:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ToggleButton android:id="@+id/ToggleButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textOff="Off" android:gravity="center" android:textOn="On"/> </LinearLayout> 
+2
source

you are watching android:drawableLeft="@drawable/the_button_resource"

There are other options, such as drawableRight / drawableTop / drawableBottom, which put a resource with a resource when a parameter.

+1
source

Source: https://habr.com/ru/post/893571/


All Articles