Create an toggle / sliding button

Leow Zheng Xu

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>
bofredo

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Not Sliding my toggle-slide button on confirm NO

From Dev

Create a toggle button

From Dev

Create a toggle button with RxJS

From Dev

How to create a tkinter toggle button?

From Dev

How to Create multiple Toggle button

From Dev

Create Rounded Toggle Button in WPF

From Dev

Create Rounded Toggle Button in WPF

From Dev

Create Toggle Button Using jQuery

From Java

Android: Create a toggle button with image and no text

From Dev

Adding a JMenuItem to JMenuBar to create a toggle button in menu

From Dev

Create toggle button behaviour to hide or show div

From Dev

Create button in Facebook API to show/hide (toggle)

From Dev

How to create toggle switch button in qt designer?

From Dev

How to create a toggle/switch button in Flutter?

From Dev

How to create Toggle button with Ajax Helper on MVC

From Dev

How i can create sliding button like Uber android app

From Dev

Jquery, sliding toggle for similar divs

From Dev

Onclick an element, sliding toggle of div

From Dev

Toggle button is not on

From Dev

How to create a toggle button that dynamically changes HTML content?

From Dev

Using VBA to create/delete chart via a toggle button

From Dev

Sliding a button in android?

From Dev

Sliding image button in android

From Dev

Create a sliding animation with Swift

From Dev

how to create a sliding menu which will fill the window width except by puller button width?

From Dev

Sliding hover menu text toggle not fitting in

From Dev

Menu not sliding out on button click

From Dev

Sliding button or text across the screen

From Dev

Toggle Button acting as an Image Button