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>
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>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments