mardi 26 mai 2015

Android- Change colour and have ripples on layout at the same time

I have a LinearLayout. I have applied ripple effect on it which is working fine. What I'm doing is when user clicks on it, it should change its colour to White (default is Gray). If clicked again, it should become Gray again. This I achieved using layout.setBackgroundColor(Color.WHITE);.

But I want both ripples and this colour toggle. If I use setBackgroundColor, the ripple effect vanishes. Any clue about how to do this?

XML for ripples (background of LinearLayout):

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://ift.tt/nIICcg"
android:color="@color/divider">
<item android:drawable="@color/background" />
</ripple>

Please suggest equivalent solution for pre-lollipop devices. The background for this LinearLayout on pre-lollipop is,

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://ift.tt/nIICcg">
<item android:drawable="@color/divider" android:state_pressed="true" />
<item android:drawable="@android:color/transparent" />
</selector>

Aucun commentaire:

Enregistrer un commentaire