ImageView selectableItemBackgroundBorderless does not render outside of view bounds
Asked Answered
C

6

16

I am using selectableItemBackgroundBorderless to add a ripple to an ImageView. My expected behaviour would be to have a circular ripple, extending the views size. Unfortunately the ripple gets cropped by the view bounds. How can this issue be solved?

Default state:

default state

Pressed state:

cropped ripple

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/nav_instruction_container"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/nav_gradient_bg"
    android:padding="20dp">
    <ImageView
        android:id="@+id/nav_sign"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_centerVertical="true"
        android:scaleType="centerInside"
        android:adjustViewBounds="true"
        />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/nav_sign"
        android:layout_marginLeft="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginEnd="24dp"
        android:textColor="@color/white"
        android:textSize="24sp"
        android:textStyle="bold"
        android:maxLines="2"
        tools:text="A644 Shudehill asdfkjasdf asdfasdf asdfsss"
        android:ellipsize="marquee"
        />

    <TextView
        android:id="@+id/subTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_toRightOf="@+id/nav_sign"
        android:layout_marginLeft="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginEnd="24dp"
        android:textColor="@color/white"
        android:textSize="17sp"
        tools:text="via Church St"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        app:srcCompat="@drawable/ic_nav_queue_sheet_icon_light"
        android:id="@+id/nav_queue"
        android:scaleType="centerInside"
        android:foreground="?attr/selectableItemBackgroundBorderless"
        android:adjustViewBounds="true"/>
</RelativeLayout>
Closet answered 27/11, 2017 at 13:0 Comment(3)
How are you getting that "pressed state" without having the clickable attribute set to the ImageView in the xml?Assist
AFAIK you don't need the clickable attribute if you attach a click listenerCloset
can you solve thisThinia
T
15

If you have a clickable ImageView, then most possibly it should be an ImageButton instead.

Having defined following ImageButton:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackgroundBorderless"
    app:srcCompat="@drawable/ic_navigation_black_24dp" />

Then you'll get following output:

If you want bigger ripple effect, you have to change view's size: instead of wrap_content make it, let's say, 100dp:

enter image description here

Typesetter answered 4/12, 2017 at 8:25 Comment(2)
Can you add a background color to your view (so I can see it's boundaries) and use the ripple as foreground?Closet
For that purpose I have turned on "show layout bounds" developer option. The square you see in gifs is the boundary of the view.Typesetter
R
3

Define ripple_effect_circle.xml in drawable

<?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="#BDBDBD"
    >

    <item android:id="@android:id/mask">
        <shape android:shape="oval">
            <size
                android:width="60dp"
                android:height="60dp"/>
            <solid android:color="#BDBDBD" />
        </shape>
    </item>

</ripple>

Define ImageView in layout

    <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_margin="60dp"
            android:src="@drawable/ripple_effect_circle"
            android:background="@drawable/your_round_image"
            />
Ready answered 8/12, 2017 at 23:30 Comment(0)
D
2

Just change from using ImageView's foreground to background: android:background="?selectableItemBackgroundBorderless". Then you should see the ripple render outside of the view bounds.

Die answered 18/11, 2018 at 10:10 Comment(0)
P
0

You can use library https://github.com/hdodenhof/CircleImageView for this issue.

Create the circular Imageview with the above library and it will be having the ripple effect as you expected.

Pony answered 10/12, 2017 at 14:26 Comment(0)
M
0

One thing is your limiting the height of the ImageView by 46dp add padding to ImageView instead of Parent layout

Manservant answered 10/12, 2017 at 15:41 Comment(0)
J
0

do layerlist drawable xml make the first item shape circle then add item selectableItemBackgroundBorderless and it will work fine

Juryrigged answered 10/12, 2017 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.