5

I am trying to change background of switch.It is changing but problem is some text is visible.

enter image description here

Below is my code

  <Switch
    android:id="@+id/switchsubsurf"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="68dp"
    android:padding="5dip"
    android:textOff="                "
    android:textOn="                 "
    android:thumb="@drawable/toggle_button"
    android:track="@drawable/toggle_bg" />
Lucifer
  • 29,392
  • 25
  • 90
  • 143
user2252192
  • 61
  • 1
  • 3

2 Answers2

14

make a drawer folder inside res

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

<item android:drawable="@drawable/toggle_on" android:state_checked="true" android:state_pressed="true"/>
<item android:drawable="@drawable/toggle_on" android:state_checked="true" android:state_focused="false"/>
<item android:drawable="@drawable/toggle_off" android:state_checked="false" android:state_pressed="true"/>
<item android:drawable="@drawable/toggle_off" android:state_checked="false" android:state_focused="false"/>

</selector>

Then do this in xml

<Switch
    android:id="@+id/saveLoginCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn=""
    android:textOff=""
    android:text=""
    android:background="@drawable/toggle_selector"
    android:thumb="@android:color/transparent"
    android:track="@android:color/transparent"
    /> 
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Uma Shankar
  • 231
  • 4
  • 11
0

Take a look at this answer:

Use Android 4.0 styled toggle-button

You can use a 2.2-compatible toggle button and stylize it to look like a switch, or really anythink you want. Just swap out the *.pngs provided in the post.

Not sure why Android went away from toggle buttons in favor of switches when they could have just changed the look and feel of the button. Anyway, I would stick with the toggle button for compatibility sake, especially if you want to change the look and feel of it anyway.

Community
  • 1
  • 1
dberm22
  • 3,187
  • 1
  • 31
  • 46