Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lollipop RippleDrawable vs Selector for Pre-Lollipop

I have buttons with different draw9patch png as background. Currently the buttons are controlled by selector which look something like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/pressed" android:state_pressed="true"/>
  <item android:drawable="@drawable/disabled" android:state_enabled="false"/>
  <item android:drawable="@drawable/focused" android:state_focused="true"/>
  <item android:drawable="@drawable/default"/>
</selector>

For the Android Lollipop they have a RippleDrawable for the touch effect, which goes something like this:

<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
    <item>
    ...
    </item>
</ripple>

Regarding the new touch ripple effect:

1: Can I set draw9patch as background for RippleDrawable?

2: How do I accomodate the above two different xml's I want to follow Material design? Do I have to fork out a new folder/layout xml for the new RippleDrawable?

like image 731
Neoh Avatar asked Jul 07 '14 09:07

Neoh


1 Answers

1) Yes. See the documentation for RippleDrawable for more details on how layers are composited, but basically you want:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
    <item android:drawable="@drawable/yourninepatch" />
</ripple>

Or to also handle the disabled state in a clean way, you might want:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
    <item>
        <selector>
            <item android:state_enabled="false">
                <nine-patch
                    android:src="@drawable/yourninepatch"
                    android:alpha="?android:attr/disabledAlpha" />
            </item>
            <item>
                <nine-patch android:src="@drawable/yourninepatch" />
            </item>
        </selector>
    </item>
</ripple>

2) Yes, you should place your ripple XML in drawable-v21.

like image 118
alanv Avatar answered Oct 11 '22 07:10

alanv