0

I tried to add ripple effect on my TextView used as button which has dark background.

I already tried

  1. android:foreground="?attr/selectableItemBackground" on TextView
  2. android:theme="@style/Base.ThemeOverlay.AppCompat.Dark" on TextView parent becuase Textview has dark background, also tried light theme.

Also i can not set TextView background as android:background="attr/selectableItemBackground because i need some dark background on textview.

Can someone give me understanding, how can i achieve this requirement.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212

2 Answers2

1

The problem is with Dark theme. Ripple color is not being visible on dark . So you should try to use a Custom theme with .

  <style name="TextViewTheme" parent="@style/Base.ThemeOverlay.AppCompat.Dark">
    <!-- Customize your theme here. -->
    <item name="colorControlHighlight">@color/colorAccent</item>
</style>

Add this theme for TextView.

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/holo_green_dark"
    android:foreground="?android:selectableItemBackground"
    android:text="Android"
    android:theme="@style/TextViewTheme"
    android:clickable="true"
    android:textSize="23sp"/>
ADM
  • 20,406
  • 11
  • 52
  • 83
  • Dude still cant see ripple effect – Khemraj Sharma Apr 08 '18 at 09:35
  • Have you put Click listener ? Cause i have tested it and it works . – ADM Apr 08 '18 at 09:47
  • Yes i use this textview as button – Khemraj Sharma Apr 08 '18 at 09:58
  • By the way it works when i put selectableitembackground as background of textview, but in that case i cant set my custom background color – Khemraj Sharma Apr 08 '18 at 09:59
  • It will work for `Button` also . Read the answer. I did not put `selectableitembackground` as `background` i used a color as background . – ADM Apr 08 '18 at 10:00
  • Dude i am using textview not button, i tried this way before you answer, and tried once more after you answered but it does not work, my textview has red background. – Khemraj Sharma Apr 08 '18 at 10:03
  • And whats the color of `colorControlHighlight` in Style ? – ADM Apr 08 '18 at 10:04
  • Its tested . Something wrong with your approach . See if `TextView` Onclick is working or not . Also add the style Entry in **v-21** – ADM Apr 08 '18 at 10:07
  • My app is completed, so all clicks are working, i was just adding some effects in app, so i needed to do this. Also i dont think selectableItemBackground needs to be override in v-21, just ripple attributes are needed to do so. – Khemraj Sharma Apr 08 '18 at 10:09
  • I ended up with using an parentview with red background and to use selectableItemBackground as background of textview. – Khemraj Sharma Apr 08 '18 at 10:10
  • `selectableItemBackground` not need to be. `colorControlHighlight` is . – ADM Apr 08 '18 at 10:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168490/discussion-between-khemraj-and-adm). – Khemraj Sharma Apr 08 '18 at 10:25
0

One can use MaterialButton like this to have ripple effect on click.

    <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" />
Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82