Android L's Ripple Effect - Touch Feedback for Buttons - Using XML
UPDATE Material Components:
With the Material Components Library it is very easy to apply a ripple.
Just use the MaterialButton
and the app:rippleColor
attribute:
<com.google.android.material.button.MaterialButton
app:rippleColor="@color/my_selector"
../>
With a selector like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="..." android:color="?attr/colorOnPrimary" android:state_pressed="true"/>
<item android:alpha="..." android:color="?attr/colorOnPrimary" android:state_focused="true" android:state_hovered="true"/>
<item android:alpha="..." android:color="?attr/colorOnPrimary" android:state_focused="true"/>
<item android:alpha="..." android:color="?attr/colorOnPrimary" android:state_hovered="true"/>
<item android:alpha="..." android:color="?attr/colorOnPrimary"/>
</selector>
Old answer
You can do something like this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ripple"
/>
Where the ripple.xml is:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="@android:id/mask">
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
Android Ripple Effect for my custom button
try this link for set the ripple effect on button:
Material effect on button with background color
Android L's Ripple Effect - Touch Feedback for Buttons - Using XML
Add ripple effect to my button with button background color?
Here is another drawable xml for those who want to add all together gradient background, corner radius and ripple effect:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimaryDark">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimaryDark" />
<corners android:radius="@dimen/button_radius_large" />
</shape>
</item>
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="@color/colorPrimaryLight"
android:startColor="@color/colorPrimary"
android:type="linear" />
<corners android:radius="@dimen/button_radius_large" />
</shape>
</item>
</ripple>
Add this to the background of your button.
<Button
...
android:background="@drawable/button_background" />
PS: this answer works for android api 21 and above.
Get ripple effect on transparent custom button view
You should use a RippleDrawable
as the background of your custom button. The RippleDrawable
has a mask layer, which is what you are looking for. To do this programmatically, you can create a RippleDrawable
like so:
float[] outerRadii = new float[8];
Arrays.fill(outerRadii, height / 2);
RoundRectShape shape = new RoundRectShape(outerRadii, null, null);
ShapeDrawable mask = new ShapeDrawable(shape);
ColorStateList stateList = ColorStateList.valueof(ContextCompat.getColor(getContext(), R.color.ripple));
setBackground(new RippleDrawable(stateList, getBackgroundDrawable(), mask);
Keep in mind that RippleDrawable
is only available for API Level 21+, so if you are looking to support older versions as well you will need to fall back to something else; possibly android.R.attr.selectableItemBackground
or a StateListDrawable
, which will give your button a basic "pressed" background for older platform versions.
Using android:itemBackground with a ripple effect
You could try to set actionOverflowMenuStyle
like below :
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="actionOverflowMenuStyle">@style/CMOptionsMenu</item>
</style>
<style name="CMOptionsMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<item name="android:popupBackground">@color/colorItemBg</item>
</style>
Android L ripple touch effect on shape?
Try that one:
ripple.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="@color/COLOR_WHILE_PRESSING" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background"></item>
</ripple>
background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/BACKGROUND_COLOR" />
<corners android:radius="6dp" />
</shape>
Or maybe this post will help you: Android L FAB Button shadow
I explained there, how to implement the new FAB button, I also used the outline for that.
Can't see click ripple effect in Android Touch events
I found a way around this. Although the ripple effect is not customisable, the fix is pretty nifty. I manually called the setPressed()
method on View
inside the ACTION_DOWN
and ACTION_UP
events and now I can see the default Android ripple effect.
case MotionEvent.ACTION_DOWN:
//icon.setColorFilter(Color.argb(80, 0, 0, 0));
v.setPressed(true);
if(mAppActionDownListener != null) {
mAppActionDownListener.onAppActionDown(appObjectList.get(position), v);
}
Log.d("COOK", "ACTION_DOWN: " + ev.getX() + ", " + ev.getY());
t1 = System.currentTimeMillis();
return true;
case MotionEvent.ACTION_UP:
v.setPressed(false);
Log.d("COOK", "ACTION_UP: " + ev.getX() + ", " + ev.getY());
t2 = System.currentTimeMillis();
if(Math.abs(t2-t1) <=300) {
//Toast.makeText(context, "Click event", Toast.LENGTH_SHORT).show();
if(mAppClickListener!=null) {
mAppClickListener.onAppClicked(appObjectList.get(position), v);
}
}
Related Topics
How to Use Android Progressbar in Determinate Mode
Transparent Alertdialog Has Black Background
Creating a PDF File in Android Programmatically and Writing in It
Android: Unable to Instantiate Activity/Classnotfoundexception
How to Determine If an Android Service Is Running in the Foreground
Popupwindow $Badtokenexception: Unable to Add Window -- Token Null Is Not Valid
Android: Webview Inside Dialog or Popup
Android: How to Use Webcam in Emulator
Finish Parent and Current Activity in Android
Android: Tabhost Without Tabactivity
Android Displaymetrics Returns Incorrect Screen Size in Pixels on Ics
Switch Keyboard Profile Programmatically
How to Crop the Parsed Image in Android
Share Image and Text Through Whatsapp or Facebook