rasen58
rasen58

Reputation: 5091

How to set text to right of image on a togglebutton

I have a togglebutton with an image, but when I change the text of the button, the text is behind the image. I added extra spaces in the setText() function, but this doesn't scale well on different screen sizes. So what can I do?

the xml for the toggle button auton_select.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

 <item android:state_checked="true" 
       android:drawable="@drawable/auto_select" /> <!-- pressed -->

 <item android:state_checked="false"
       android:drawable="@drawable/auto_unselect" /> <!-- unchecked -->

 <item android:drawable="@drawable/auto_unselect" /> <!-- default -->

</selector>

using the togglebutton xml in the layout

<ToggleButton
        android:id="@+id/auto_ring_button" 
        android:textOn="               test"
        android:textOff=" "
        android:disabledAlpha=".9"
        android:button="@drawable/auton_select"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:onClick="showPopupR"
        />

Upvotes: 2

Views: 2504

Answers (1)

Sergey Glotov
Sergey Glotov

Reputation: 20356

Try to set image as compound drawable instead of background.

toggleButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.image, 0, 0, 0);

or in XML

android:drawableLeft="@drawable/image"

References:
setCompoundDrawablesWithIntrinsicBounds()
android:drawableLeft

Upvotes: 5

Related Questions