Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background of switch in android ?

Tags:

android

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" />
like image 558
user2252192 Avatar asked Mar 04 '14 12:03

user2252192


People also ask

How can I change background color of clicking button in Android?

Inside the function use setBackgroundResource(R. color. button_color) function, this will set the background with color button_color.

How do I make a custom switch button?

Right-click on the drawable folder, go to new, and click on Drawable Resource File. Now set the name as custom_switch, root element as the selector, and click OK. Now add the below code to your file. The below code represents two states on the Switch, when true and when false.


1 Answers

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"
    /> 
like image 93
Uma Shankar Avatar answered Nov 02 '22 12:11

Uma Shankar