3

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

ankuranurag2
  • 2,300
  • 15
  • 30
Ashpak Kureshi
  • 93
  • 2
  • 10
  • try `android:background="?attr/selectableItemBackgroundBorderless"` https://stackoverflow.com/a/34128965/7666442 – AskNilesh Feb 19 '19 at 10:22
  • Add the ripple-like this I have used this before : [set ripple effect ](https://stackoverflow.com/questions/31393684/set-ripple-effect-on-image-view/35753159#35753159) – Ram Mohan dubey Feb 19 '19 at 10:24
  • but i have already added background so now how to add ripple – Ashpak Kureshi Feb 19 '19 at 10:31
  • The solutions provided in answers are enough to create ripple effect. But it will only appear when you `setOnClickListener` or make the `view` clickable. – ankuranurag2 Feb 19 '19 at 13:20

6 Answers6

10

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"
Rahul
  • 3,293
  • 2
  • 31
  • 43
Kartik Shah
  • 866
  • 5
  • 19
6

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

Vishal Arora
  • 2,524
  • 2
  • 11
  • 14
  • 1
    Your textview should be clickable for ripple to work. Either set `android:clickable="true"` or set a click listener in code. – Vishal Arora Feb 19 '19 at 10:52
  • Please add some explanation to your code such that others can learn from it - why should one "use" that given stuff? – Nico Haase Feb 19 '19 at 11:35
2

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

android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
Ghayas
  • 1,266
  • 10
  • 17
1

enter image description here

use this library for best ripple effect.. link

Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25
0

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"
Ashik Azeez
  • 404
  • 5
  • 8
-1

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.

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82