Background
On the dialer app of Android, when you start searching for something, and you click the arrow button on the left of the EditText, you get a circular ripple effect on it :
The problem
I've tried to have it too, but I got a rectangular one:
<ImageButton
android:id="@+id/navButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:background="?android:attr/selectableItemBackground"
android:src="@drawable/search_ic_back_arrow"/>
The question
How do I make the button have a circular ripple effect when being clicked? Do I have to create a new drawable, or is there a built in way for that?
You can create circle ripple drawable using
android:radius
attribute in xml.Example:
Pay attention, that your
your_radius
should be less then your view width and height. For example: if you have view with size60dp x 60dp
your_radius
should be near30dp
(width / 2 or height / 2).Another attribute with round ripple effect, specially for action bar:
If you want more generic XML files, I have two files:
1) btn_ripple_background with code:
2) ripple_circuler_shape with code:
finally the usage:
android:foreground="@drawable/ripple_btn_background"
If you are using AppCompat theme, then you could set the background of the view as:
This will add circular ripple on 21 and above and square background on below 21.
If you already have a background image, here is an example of a ripple that looks close to selectableItemBackgroundBorderless:
ic_filter_state.xml:
state_pressed_ripple.xml: (opacity set to 10% on white background) 1AFFFFFF
Just Add this background to your view
"android:background=?android:attr/actionBarItemBackground"