Android ToggleButton

The dimensions are "hardcoded" but they'll scale with the size of your screen.

So the xml could look like:

android:layout_width="64dp"
android:layout_height="24dp"

The official documentation for dps is here


Try following attributes for <ToggleButton>

android:background="@android:color/transparent"
android:button="@drawable/toggle_bg"

It should work. Good luck :)


To support versions from GINGERBREAD up to the current ones, I ended up using:

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/filterPhotos"
    android:checked="true"
    android:textOn=""
    android:textOff=""
    android:paddingLeft="5dp"
    android:drawableRight="@drawable/toggle_filter_photos"
    android:background="@null"/>

and the drawable toggle_filter_photos.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/picture_filter_icons"
          android:state_checked="true" />
    <item android:drawable="@drawable/picture_inactive_filter_icons" />
</selector>

The other solutions above didn't work well for GINGERBREAD and newer as either the icons wouldn't show up at all (with android:button on GINGERBREAD) or the aspect ratio was lost (with android:background on JELLY_BEAN_MR1 (4.2.2)).

Tags:

Android

View