Here is my custom ripple
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:drawable="#color/white" />
</ripple>
How do I make it borderless?
As per the RippleDrawable documentation, a ripple will be masked against the composite of its child layers.
In this case, your drawable will be masked by the only item in your ripple.
To have a ripple with no mask, you can define your ripple like so:
<ripple android:color="?android:colorControlHighlight" />
I had the same problem, while I stumbled upon this question. I have solved my problem easily and there is a code
Btw it works only for cases when height equals width. Else it will be not ?attr/selectableItemBackgroundBorderless, but ripple oval
<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="#color/grayPrimary" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/mask">
<shape android:shape="oval">
<solid android:color="#000000"/>
</shape>
</item>
</ripple>
Related
How to implement following hover (Ripple) in android?
I have a linear layout and i want when user touched the layout all widgets in linear layout become lighter.
Create a ripple drawable in res/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="#color/ripple"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#color/ripple" />
<corners android:radius="#dimen/dp_6" />
</shape>
</item>
</ripple>
Set this drawable to the button foreground:
android:foreground = "#drawable/bg_btn_ripple_animation"
You can modify the ripple drawable as per your liking.
When you look at other apps like yPlan or Google Play Store, their ripple effect on buttons is like a light grey or black that does not change the colour of the whole button.
I want this same effect.
Here is what i tried but its not the same
This is my colour control highlight: Its a grey with 99% alpha
<color name="colorHighlight">#fc8c969f</color>
And this is my ripple drawer for my button
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="#android:id/mask" android:drawable="#android:color/white"/>
<item android:drawable="#color/colorAccentWith92PercentOpacity"/>
</ripple>
Note the drawable is to make the colour of the button yellow since i cant use android:background twice
<?xml version="1.0" encoding="utf-8"?>
<!--Use an almost transparent color for the ripple itself-->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#22000000">
<!--Use this to define the shape of the ripple effect (rectangle, oval, ring or line). The color specified here isn't used anyway-->
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<!--This is the background for your button-->
<item>
<!--Use the shape you want here-->
<shape android:shape="rectangle">
<!--Use the solid tag to define the background color you want (here yellow)-->
<solid android:color="#ffff00"/>
<!--Use the stroke tag for a border-->
<stroke android:width="1dp" android:color="#ffff00"/>
</shape>
</item>
</ripple>
When using the selectableItemBackground attribute for "touch ripple effects", it seems that normal touches are too fast for the "touch ripple" to appear. I have to touch a bit longer and everything works as it should. Is it possible to force this effect some how, even on short touches?
I should suggest to you that create a custom ripple effect.
Selector for Pre API 21
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/colorAccent" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent" />
</shape>
</item>
</selector>
Ripple Effect for API 21+ Devices
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorAccent">
<item
android:id="#android:id/mask"
android:drawable="#android:color/white" />
</ripple>
For More: Click Here
Something like the following, but it doesn't work. If I switch the drawable color to something like blue, it works.
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:drawable="#android:color/transparent"/>
</ripple>
It's necessary to add a mask:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="#android:id/mask">
<color android:color="#android:color/white" />
</item>
</ripple>
Somehow, the #JMPergar 's answer didn't work for me.
However, I was able to think of this workaround: if a colour behind your transparent button is solid (not a gradient or a speckled picture) - you can use that color as a main button's unpressed one.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/profile_transparent_button_pressed"
>
<item>
<shape>
<corners android:radius="#dimen/profile_transparent_button_corner_radius" />
<solid android:color="#color/profile_background" />
</shape>
</item>
</ripple>
where #color/profile_transparent_button_pressed is a colour to highlight a button, and #color/profile_background - the colour of a layout behind this button. It works exactly as expected, but still definitely is a workaround so you go try #JMPergar 's answer first.
I'm a bit lost about how to properly use Ripple Drawable.
Let's say I have this drawable :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:state_focused="true" android:drawable="#color/accent_color_light" />
<item android:state_pressed="true" android:drawable="#color/accent_color_light" />
<item android:drawable="#android:color/white" />
</selector>
So it is a plain white background which changes to a light blue when it is focused or pressed.
How can I get the same colors but with a ripple effect ?
I think I need to use a mask to prevent it from getting outside the bounds of the view ?
Forgot to answer my own question.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_shortAnimTime"
android:color="#color/my_color" >
<item android:id="#android:id/mask">
<shape android:shape="rectangle" >
<solid android:color="#android:color/holo_green_light" />
</shape>
</item>
</ripple>
The color in the item with the id "mask" is not displayed. It is used to tell the shape and bounds of the ripple effect. Without it, it can go outside the bounds of the view.
RippleDrawable is already a StateListDrawable (ie selector) - so you can just use a ripple drawable as your background (with a default state) - something like this:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/accent_color_light">
<item android:id="#android:id/mask">
<color android:color="#android:color/white" />
</item>
<item android:drawable="#android:color/white" />
</ripple>
the mask piece bounds the ripple (and, in reality, the above snippet, minus the colors and the last android:drawable which sets the non-pressed background) is the default list selector used in lollipop.