Create an toggle / sliding button - android

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>

Related

Adding a BackgroundTint on a ToggleButton messes the Background itself

I have a layout with multiple ToggleButtons with a custom layout. Each of them must have a different color.
To achieve that I've created a selector with 2 items in it for the checked and unchecked state. The cheked item has a drawable inside. So I have the xml's like this:
<ToggleButton
android:id="#+id/color_picker_btn_1"
android:layout_width="38dp"
android:layout_height="38dp"
android:textOff=""
android:textOn=""
android:layout_marginEnd="12dp"
android:checked="true"
android:background="#drawable/selector_button_color"/>
The selector_button_color:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_circle_checked" android:state_checked="true"/>
<item android:drawable="#drawable/shape_circle"/>
</selector>
The shape_circle_checked:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:visible="true">
<solid android:color="#color/colorPrimary" />
</shape>
</item>
<item android:drawable="#drawable/ic_check">
</item>
The problem is that when I apply a background tint on the ToggleButton I can't see the drawable on the Checked state.
I would like to know if there is a way to select the background color of each ToggleButton without messing up the background itself with the drawable. Otherwise I will have to create a custom background for every single ToggleButton.
Thank you very much!
To solve it I created an SVG with the circle and the check. That does the trick!
You can use something like:
<com.google.android.material.button.MaterialButton
android:id="#+id/toogleButtton1"
style="#style/circleButton.Green"
android:layout_width="48dp"
android:layout_height="48dp"
/>
<com.google.android.material.button.MaterialButton
android:id="#+id/toogleButtton2"
style="#style/circleButton.Red"
android:layout_width="48dp"
android:layout_height="48dp"
/>
<com.google.android.material.button.MaterialButton
android:id="#+id/toogleButtton3"
style="#style/circleButton"
android:layout_width="48dp"
android:layout_height="48dp"
/>
with:
<style name="circleButton" parent="Widget.MaterialComponents.Button">
<item name="shapeAppearanceOverlay">#style/circle</item>
<item name="icon">#drawable/ic_add_24px</item>
<item name="iconTint">#color/custom_button_icontint_selector</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
<item name="android:padding">12dp</item>
<item name="android:checkable">true</item>
</style>
<style name="circleButton.Green">
<item name="materialThemeOverlay">#style/ThemeOverlay.Green</item>
</style>
<style name="ThemeOverlay.Green" parent="">
<item name="colorPrimary">#color/green500</item>
</style>
<style name="circleButton.Red">
<item name="materialThemeOverlay">#style/ThemeOverlay.Red</item>
</style>
<style name="ThemeOverlay.Red" parent="">
<item name="colorPrimary">#color/red600</item>
</style>
<!-- Circular ShapeAppearance -->
<style name="circle">
<item name="cornerSize">50%</item>
</style>
<!-- custom selector for icon tint to hide the icon when unchecked -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorOnPrimary" android:state_checked="true"/>
<item android:color="?attr/colorPrimary" android:state_checked="false"/>
</selector>

Button Style background color not working - XML only

Im trying to set a background color from the style xml depending if the state of the button is enabled or not.
Button XML:
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/loginButton"
style="#style/SuccessButton2"
android:layout_width="200dp"
android:layout_height="40dp"
android:text="Iniciar sesiĆ³n"
android:gravity="center"
android:enabled="true"
/>
Style SuccesButton2:
<style name="SuccessButton2" parent="TextAppearance.MaterialComponents.Button">
<item name="android:textColor">#color/button_text_color</item>
<item name="android:textSize">16sp</item>
<item name="android:background">#color/button_background_color</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:textAllCaps">false</item>
<item name="fontFamily">#font/myriad_pro_regular</item>
</style>
XML button_background_color:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#color/dark_gray"/>
<item android:state_enabled="true" android:color="#color/red"/>
i have the same for the Text and its working fine:
button_text_color:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#4E4E4E"/>
<item android:state_enabled="true" android:color="#android:color/black"/>
The background item its not working, it show the button with a transparent background only, enabled or not.
You can try with android:backgroundTint .
Tint to apply to the background.
<item name="android:background">#color/button_background_color</item>
<item name="android:backgroundTint">#color/button_background_color</item>

How to change android AppCompatButton's text color on pressed

I have created following style for custom AppCompatButton.
<style name="AppTheme.secondary_button" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/secondary_button_color</item>
<item name="colorControlActivated">#color/menu_blue</item>
<item name="android:colorButtonNormal">#color/secondary_button_color</item>
<item name="android:textColorPrimary">#color/gray</item>
<item name="android:textColor">#color/gray</item>
</style>
What is the attribute to change text color on button pressed?
To do this, you just need to replace the textColor you specified with a selector:
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/white" />
<item android:state_focused="true" android:color="#color/grey" /> <!-- (for accessibility) -->
<item android:color="#color/black" />
</selector>
and then assign it like this:
android:textColor="#drawable/button_foreground_selector"
<android.support.v7.widget.AppCompatButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="#style/CustomAppCompatButtonStyle"
android:text="Hi, Neo"/>
Not clicked button:
Clicked button:
Style I used:
<style name="CustomAppCompatButtonStyle" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/red</item>
<item name="colorControlActivated">#color/blue</item>
<item name="android:colorButtonNormal">#color/green</item>
<item name="android:textColorPrimary">#color/gray</item>
<item name="android:textColor">#drawable/button_foreground_selector</item>
</style>

Change the color of a disabled button in android

Is there a way to change the color of a disabled button in android through styles or some other form ?
I currently have the following,
drawable/button_default.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_default_shape"/>
</selector>
drawable/button_default_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/button_default_background"/>
<corners android:radius="3dp"/>
</shape>
values/styles.xml
<style name="AppTheme.Button">
<item name="android:background">#drawable/button_default</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textAllCaps">false</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">17sp</item>
<item name="android:textAppearance">#style/CustomFontAppearance</item>
</style>
You'll have to use a selector for different drawables in those states.
You can create a selector like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/your_enabled_drawable" android:state_enabled="true" />
<item android:drawable="#drawable/your_disabled_drawable" android:state_enabled="false" />
<!-- default state-->
<item android:drawable="#drawable/your_enabled_drawable" />
</selector>
Specify the color in selector for android:state_enabled="false" as drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<solid android:color="#32ff09" />
</shape>
</item>
</selector>
And apply this drawable resource to background of button
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/drawble_name"
android:enabled="false"
android:text="Selector Applied Button" />
Try this -
drawable/bg_button.xml :-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_button_focused" android:state_selected="true"/>
<item android:drawable="#drawable/bg_button_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/bg_button_disabled" android:state_enabled="false" />
<item android:drawable="#drawable/bg_button_normal"/>
</selector>
After this just set background on your button like this -
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_button"
android:text="Button"/>
UPDATE: For Android MaterialButton
For Android MaterialButton there are two solutions, one for SDK >= v21 and another for SDK < v21.
Here I am also giving how to change the text color of the button depending whether it is enabled or disabled.
First create two color selector .xml file in your color folder. One for button color and another for button text color. For example button_state_colors.xml for button color and button_text_state_colors.xml for text color.
Here are those two color files:
button_state_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#303F9F" />
<item android:state_enabled="false" android:color="#BBBBBB" />
</selector>
button_text_state_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#FFFFFF" />
<item android:state_enabled="false" android:color="#555555" />
</selector>
Now for all versions of Android SDK you can change the color of button and it's text using style like:
in style.xml:
<style name="Widget.AppTheme.MaterialButton" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">#color/button_state_colors</item>
<item name="android:textColor">#color/button_text_state_colors</item>
</style>
in layout.xml:
<com.google.android.material.button.MaterialButton
android:id="#+id/button"
style="#style/Widget.AppTheme.MaterialButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Only for SDK >= 21 you can direct change button color without using style like:
<com.google.android.material.button.MaterialButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/button_state_colors"/>
Another way to achieve this is to create a color selector.
Create a file
res/color/your_color.xml
which looks like
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorDisabledBackground" android:state_enabled="false" />
<item android:color="#color/colorEnabledBackground" />
</selector>
Then you may use it as a regular color
in a style:
<style name="YourStyle">
<item name="android:background">#color/your_color</item>
<item name="android:textColor">#android:color/black</item>
</style>
As well is in layouts, shapes or in code as etc.
Here is my code which works properly with button enable and disable state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="#drawable/ic_button_gradient"/>
<item android:state_enabled="false" android:drawable="#color/gray"/>
</selector>

Styling a switch

I want to use a switch in my app, but I want it to have custom colors. Right now it doesn't show up at all.
Switch in my xml:
<Switch
android:id="#+id/switch"
android:track="#drawable/switch_track"
android:thumb="#drawable/switch_thumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
switch_track.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#color/grey_light"/>
<item android:state_pressed="true" android:drawable="#color/grey_light"/>
<item android:state_checked="true" android:drawable="#color/accent_light"/>
<item android:drawable="#color/grey_light"/>
</selector>
switch_thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#color/grey"/>
<item android:state_pressed="true" android:drawable="#color/grey"/>
<item android:state_checked="true" android:drawable="#color/accent"/>
<item android:drawable="#color/grey"/>
</selector>
Do I need to add icons for the selectors instead of colors or is there a way to just change the color?
If you are using the support library, you could use the material properties to color a SwitchCompat:
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
styles.xml:
<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
<item name="colorSwitchThumbNormal">#color/grey</item>
<item name="colorControlActivated">#color/accent</item>
</style>

Categories

Resources