I tried to track down, how Lollipop displays a button, which is disabled with android:enabled="false"
in the layout file.
With Holo, it's easy: In the styles_holo.xml, I find the style Widget.Holo.Button, which gives me a reference to @drawable/btn_default_holo_dark. There I find the selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/btn_default_normal_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/btn_default_disabled_holo_dark" />
<item android:state_pressed="true"
android:drawable="@drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/btn_default_focused_holo_dark" />
<item android:state_enabled="true"
android:drawable="@drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true"
android:drawable="@drawable/btn_default_disabled_focused_holo_dark" />
<item
android:drawable="@drawable/btn_default_disabled_holo_dark" />
</selector>
When I try to apply the same logic to Lollipop, I got stuck:
In styles_material.xml I find the style <style name="Widget.Material.Button">
where I find the reference to <item name="background">@drawable/btn_default_material</item>
. But there is no selector??!! Instead I find:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:drawable="@drawable/btn_default_mtrl_shape" />
</ripple>
Could someone please explain, which specific style Lollipop uses for a disabled button. Thanks a lot!
I can partially answer myself: In @drawable/btn_default_mtrl_shape
I find a reference to <solid android:color="?attr/colorButtonNormal" />
, which in turn points to @color/btn_default_material_light
, which includes a selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:alpha="@dimen/disabled_alpha_material_light"
android:color="@color/button_material_light"/>
<item android:color="@color/button_material_light"/>
</selector>
But that alpha value only explains half of it. Somehow Lollipop also set the elevation down to 0?
This is how I resolved this, thanks to your partially answer.
First: add new Folder "color" under "res" folder, if it doesn`t exist.
Add new .xml file in "color" folder (I will call this file ButtonColorSelector.xml) where we will create new ColorStateList like this:
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#F2F2F2"/> <!-- disabled -->
<item android:state_pressed="true" android:color="#FF0000"/> <!-- pressed -->
<item android:state_focused="true" android:color="#FF0000"/> <!-- focused -->
<item android:color="#0000FF"/> <!-- default -->
</selector>
Second: Add that ripple effect .xml file you mentioned, under "drawable" folder and reference your colorSelector instead of btn_default_mtrl_shape. I will call this file RaisedButton.xml.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item>
<shape>
<solid android:color="@color/buttoncolorselector"/>
<corners android:radius="5dp" />
</shape>
</item>
</ripple>
Third: Now you can use your drawable for button background in your layouts, like this:
<Button
android:background="@drawable/raisedbutton"
android:text="@string/SomeButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="DoStuffitto"
android:enabled="false"
android:id="@+id/someButton" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With