Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lollipop: Disabled button --> which style?

I tried to track down, how Lollipop displays a button, which is disabled with android:enabled="false" in the layout file.

Holo

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>

Lollipop

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!

Edit

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?

like image 320
Micha Brix Avatar asked Jan 13 '15 16:01

Micha Brix


1 Answers

This is how I resolved this, thanks to your partially answer.

First: add new Folder "color" under "res" folder, if it doesn`t exist.enter image description here

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" />
like image 117
DieBunny Avatar answered Oct 27 '22 13:10

DieBunny