seang96
seang96

Reputation: 61

Why is this ripple effect is not working as expected?

I am using a drawable to change the background and text of textViews inside of my navigation drawer. I would like to keep the background white for the text area, but by testing keeping the background white does not show the ripple effect on the background, instead it does it to the text making the text gray. In the picture below, the middle one is being pressed causing the ripple effect.

The ripple effect

Here are my drawable files that are used to make the changes in colors

Background:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/selected" android:state_activated="true" />
    <item android:drawable="@color/white" />
</selector>

Text:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/primary" android:state_activated="true" />
    <item android:color="@color/primary_text" />
</selector>

textView layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25.0sp"
        android:background="@drawable/activated_background"
        android:textColor="@drawable/activated_text"
        android:id="@id/text1"
        android:layout_weight="0"
        android:layout_marginTop="8dip"
        android:layout_marginBottom="8dip" />

</RelativeLayout>

Upvotes: 1

Views: 2764

Answers (3)

anand krish
anand krish

Reputation: 4415

In my case ripple effect is working after the first click, but for first click it didn't work for me. Have changed the background selector file with android:state_activated="true" and in main.xml android:clickable="true" then it's work fine for all time.

selector.xml (under res\drawable\selector.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@drawable/card_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:state_activated="true" android:drawable="@drawable/card_bg_focused" android:state_enabled="true" android:state_focused="true"/>
<item android:state_activated="true" android:drawable="@drawable/card_bg_selected" android:state_enabled="false" android:state_selected="true"/>
</selector>

In activity_main.xml

 <com.mysample.RecyclingImageView
    android:id="@+id/imageview_overlay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/selector"
    android:clickable="true" />

Upvotes: 0

Milad Nouri
Milad Nouri

Reputation: 1597

you should use 2 drawable files and use it as your view background. for pre-lolipop versions:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/white_list_item_selected_background" android:state_pressed="true" />
    <item android:drawable="@android:color/white" />

</selector>

for lolipop (v21):

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

</ripple>

Upvotes: 2

Myat Min Soe
Myat Min Soe

Reputation: 807

If you use custom background, ripple effect will not be shown. You may have to use other ripple library such as Material Ripple.

Upvotes: 0

Related Questions