Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ripples with border for a TextView?

Tags:

android

ripple

I have few TextViews that are displayed beside each other. Currently, I use android:background="?attr/selectableItemBackground" to get the ripple on touch.

Now, I would like to add borders to the TextViews when they are not pressed and have the ripple when the TextView is touched. How do I do that?

like image 882
An SO User Avatar asked Mar 03 '16 15:03

An SO User


People also ask

How do you add a border to TextView?

To add a border to Android TextView we need to create an XML containing shape as a rectangle file under the drawable's folder and set it as background to the TextView. <stroke> tag is used to set the border width and color.

What is ripple effect in Android?

The touch feedback in Android is a must whenever the user clicks on the item or button ripple effect when clicking on the same, gives confidence to the user that the button has been clicked so that they can wait for the next interaction of the app.


1 Answers

Based on a Styling Android blog post:

Another way that we can confine the bounds of a ripple animation is to actually define a shape drawable as a child

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <stroke
                android:color="@color/card_set_bg_color"
                android:width="1dp" />
        </shape>
    </item>
</ripple>  

Here the <solid> in <shape> is needed for the ripple to render. If set to transparent or omitted, the ripple doesn't render. The ripple needs a solid background to render on. TextViews don't have a background color so we need to specify a <solid>.

like image 194
An SO User Avatar answered Oct 14 '22 05:10

An SO User