1

I have follow the answer in Android: Create a toggle button with image and no text everything was fine but when I load my application and when I click on my toggle button, nothing happen like it didn't change from off state to on state. So what do i have to do to make the sliding works? Do i need to code anything if yes what should i code? Thanks btn_toggle_bg.xml

<?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/btn_toggle" />
</layer-list>

btn_toggle.xml

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

layout.xml

           <ToggleButton
               android:id="@+id/toggleButton"
               style="@style/toggleButton"
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:background="@drawable/btn_toggle_bg"
                android:onClick="onToggleClicked"
               android:layout_marginLeft="150dp" />

style.xml

<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">@drawable/btn_toggle_bg</item>

<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
<style name="toggleButton"  parent="@android:Theme.Black">
 <item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item>

</style>
Community
  • 1
  • 1

1 Answers1

1

Follow the three steps and after it all your app's togglebutton got the new style!

toggle.xml: (drawable-folder)

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

styles.xml: (values-folder)

<style name="toggle" parent="@android:style/Widget.Button.Toggle">
    <item name="android:background">@drawable/toggle</item>
    <item name="android:textOn"> </item>
    <item name="android:textOff"> </item>
</style>

and last step you have to teach the current AppTheme that you have a custom toggle-button:

<style name="AppTheme" parent="android:Theme.Holo.Light">
    <item name="android:buttonStyleToggle">@style/toggle</item>
</style>
bofredo
  • 2,348
  • 6
  • 32
  • 51
  • Yup i think i did this 3 step correctly problem is how to code it so it can change from off to open when i click on it . Like what do i write in my main activity, hope you can help cheers – Leow Zheng Xu Oct 30 '13 at 11:10
  • You just want to use the standard-toggle-button and change "On/Off" to "On/Open"!?! now i am confused :)# – bofredo Oct 30 '13 at 11:13
  • I wanted to implement an On/Off customize design on my own. The problem now is when i run the application and click on the toggle button it doesn't move from off to on. So i was wondering where did i do wrong, is it because i didn't code it or? Sorry if i have confuse you( i have poor english) – Leow Zheng Xu Oct 30 '13 at 11:17
  • just curious. did you initialize the button-listener in your activity? i don't see the error here. anyways, lunch now :) – bofredo Oct 30 '13 at 11:28
  • Nope i didn't . How do i do it? – Leow Zheng Xu Oct 31 '13 at 01:34
  • ahh. looks like that you found the source of evil :-) – bofredo Oct 31 '13 at 09:27