Ashpak Kureshi
Ashpak Kureshi

Reputation: 93

how to apply ripple effect on textview when already background set

I want to apply ripple effect when any item click. But I can't apply each and every item

android:background="@color/tabColor"

background already set so how to use following code

android:background="?android:attr/selectableItemBackground"
<TextView    
            android:id="@+id/mistake_btn_tv"    
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            android:background="@color/tabColor"
            android:gravity="center"
            android:requiresFadingEdge="none"
            android:text=""
            android:textColor="@color/md_white_1000"
            android:textSize="@dimen/txt_20"
            android:textStyle="bold"
            android:typeface="monospace"/>

I expect both are working there, my background "tabColor" and also ripple effect

Upvotes: 3

Views: 4792

Answers (6)

Kishan Solanki
Kishan Solanki

Reputation: 14636

I am late to the party but above solutions are not working for me. So I am using MaterialButton like this,

    <com.google.android.material.button.MaterialButton
        style="?attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:paddingHorizontal="@dimen/padding_8dp"
        android:text="Clickable Text"
        android:textColor="your text color"
        app:backgroundTint="@android:color/transparent"
        app:rippleColor="ripple effect color" />

Here, style="?attr/buttonBarButtonStyle" and app:backgroundTint="@android:color/transparent" will make this button as transparent background so that it will look like TextView and everything else will be done automatically.

Upvotes: -1

Ghayas
Ghayas

Reputation: 1324

when you have set background already do this in your TextView XML code.

android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackgroundBorderless"

Upvotes: 2

Kartik Shah
Kartik Shah

Reputation: 916

You can use a custom drawable file for ripple effect. Here I am sharing code with you. Just implement that code. Hope it will work fine.

  <?xml version="1.0" encoding="UTF-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:color="@color/colorAccent"
    tools:ignore="NewApi">
    <item android:id="@android:id/background">
        <shape xmlns:android="http://schemas.android.com/apk/res/android">

            <solid android:color="@color/white"/>
            <corners
                android:radius="5dp"/>
            <stroke android:width="1dp" android:color="@color/gray_text"/>
            <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />

        </shape>
    </item>
</ripple>

and set it to background:-

  android:background="@drawable/border_ripple_gray"

Upvotes: 11

Mayur Dabhi
Mayur Dabhi

Reputation: 3936

enter image description here

use this library for best ripple effect.. link

Upvotes: 1

Vishal Arora
Vishal Arora

Reputation: 2564

You should use android:foreground="?attr/selectableItemBackground" for ripple effect.

Upvotes: 6

Ashik Azeez
Ashik Azeez

Reputation: 444

Add this line in your style.xml

<item name="android:colorControlHighlight" tools:targetApi="lollipop">@color/backgroundWhite</item>

and use view property:-

android:foreground="?attr/selectableItemBackground"

Upvotes: 0

Related Questions