Android toggle button custom look
Asked Answered
E

4

61

I've been trying to customize the toggle button look but with no success. Here is how I want it to look like:

enter image description here

Can someone give me a tutorial?

Etude answered 20/8, 2013 at 12:36 Comment(5)
see https://mcmap.net/q/324085/-slide-toggle-for-androidDuhon
not what I'm looking for i need something for 2.2Etude
As CWAC said, Android is opensource, just go into source of 4 and backport the feature you like within your application to 2.XDuhon
ok, but is there a way without using a switch?Etude
Have a look on this - #23359322Overkill
G
120

create toggle_selector.xml in res/drawable

<?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"/>
  <item android:drawable="@drawable/toggle_off" android:state_checked="false"/>
</selector>

apply the selector to your toggle button

<ToggleButton
            android:id="@+id/chkState"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/toggle_selector"
            android:textOff=""
            android:textOn=""/>

Note: for removing the text i used following in above code

textOff=""
textOn=""
Gymnasiast answered 30/11, 2013 at 13:22 Comment(0)
M
9

I don't know if this is the best solution but it worked fine for me:

1.- Decide how big do you want the toggle button. In my case width 56dp and height 76dp.

2.- Create the icon set 56px-76px for mdpi, 84px-113px hdpi, same for xhdpi and xxhdpi

3.- Move the icons in the corresponding drawable folder. In my case 20 icons 5 in each folder, named ic_name1_on, ic_name1_off [...] ic_name5_off

4.- Create the following xml files in a new folder called drawable (if it does not exist yet):

  • ic_name1_toggle.xml
  • ic_name1_toggle_bg.xml
  • ic_name2_toggle.xml
  • (...)
  • ic_name5_toggle_bg.xml

5.- In ic_name1_toggle.xml the code must be:

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

6.- In ic_name1_toggle_bg.xml the code must be:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+android:id/background"
         android:drawable="@android:color/transparent" />
   <item android:id="@+android:id/toggle"
         android:drawable="@drawable/ic_name1_toggle" />
</layer-list>

7.- Finally in your layout.xml:

<ToggleButton
                android:id="@+id/toggleButton1"
                android:layout_width="56dp"
                android:layout_height="76dp"
                android:background="@android:color/transparent"
                android:button="@drawable/ic_name1_toggle_bg"
                android:textOff=""
                android:textOn="" />
Misuse answered 28/7, 2014 at 15:41 Comment(0)
S
1

I think you need to define a custom background for your button. Take a look at the Developer Guide on customizing a Button's background.

However, in step Three, Create a new XML file in the res/drawable/ directory Use this Xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_da"
          android:state_checked="true" />
    <item android:drawable="@drawable/button_nu" />
</selector>

The element android:state_checked="true" is what defines that state as the checked background.

Let me know if this works for you.

Smatter answered 11/9, 2013 at 20:35 Comment(0)
S
0

Create selector

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

and use it as background for your ToggleButton.

Sampler answered 20/8, 2013 at 13:41 Comment(2)
I've kept trying this but the code shows error or does nothing, could you please be a bit more comprehensive? What exactly do I have to do? I'm kind of a beginner.Etude
You have to create two drawables (btn_da and btn_nu)Inflammable

© 2022 - 2024 — McMap. All rights reserved.