Set the background in the TextView and keep the ripple effect

I want to set the background of a TextView object programmatically, but also have a ripple effect for it.

I can use with the background set to android:selectableItemBackground, but the ripple effect is lost when setting the background.

I also tried to put ImageViewalong with TextViewat FrameLayout. And set the image not as a background TextView, but as an image ImageView: Yes, there is ripple, but it seems “behind” the image.

Do I need to create RippleDrawablefrom a bitmap for the background? How should I do it?

+4
source share
2

, , , android:foreground="?attr/selectableItemBackground" FrameLayout.

FrameLayout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/more"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:foreground="?attr/selectableItemBackground">
    <ImageView
        android:id="@+id/discover_carousel_item_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical|center_horizontal"
        android:scaleType="fitXY" />
</FrameLayout>
+5

RippleDrawable - :

RippleDrawable newImage = new RippleDrawable(
        new ColorStateList(
                new int[][]{
                        new int[]{android.R.attr.state_pressed},
                        new int[]{}
                },
                new int[] {
                        getResources().getColor(R.color.ripple_material_light),
                        getResources().getColor(R.color.ripple_material_dark),
                }),
                new BitmapDrawable(getResources(), mapBitmap),
                null);
textView.setBackground(newImage);
0

Source: https://habr.com/ru/post/1568447/


All Articles