How to add ripple animation to widget with custom background? - android

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.

Related

how to add gradient color to vector image in android and to use in bottom navigation view

how to achieve this bottom navigation view in android with gradient color effect.
Incase you dont have gradient drawable, here is the example of primary_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#color/white"
android:endColor="#color/colorPrimary"
android:angle="90" />
</shape>
You need to make a selector for it, lets call it nav_item_background_state.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/primary_gradient" android:state_checked="true" />
<item android:drawable="#color/white" android:state_checked="false" />
</selector>
Then you can add code below to your bottom navigation
app:itemBackground="#drawable/nav_item_background_state"
you can use gradient color in vector but drawable is not allowed
for using gradient color you have to create a file inside res/color folder
<?xml version="1.0" encoding="utf-8"?>
<gradient xmlns:android="http://schemas.android.com/apk/res/android"
android:startColor="#FFFFFF"
android:centerColor="#0000FF"
android:endColor="#00FFFF"
android:angle="145"
android:startX="30"
android:endX="70"
android:startY="30"
android:endY="70"
android:type="linear"/>
and inside vector file use it as a normal color

Apply ripple effect on click of a relative layout which already has a background XML

I am trying to implement a ripple effect on click of a relative layout which already has a background XML. But how can I do this? If I want to add the ripple effect to the relative layout then I must change the tag in the background XML to <ripple></ripple>, but when I do this I lose the corner radius and solid color that I had given for the relative layout.
Below is the code of the relative layout:
<RelativeLayout
android:layout_width="400dp"
android:layout_height="100dp"
android:background="#drawable/roundedcorners">
</RelativeLayout>
Below is the code in the background XML "roundedcorners"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#000000"/>
<corners
android:radius="25dp"/>
</shape>
I looked out for posts for the same but did not find one, as the posts had just the information on how to achieve the ripple effect, but not on how to merge and use the ripple effect, scale, or shape tags together.
The solution for this lies in the special mask layer of the RippleDrawable. You specify the mask layer via the android:id value set to #android:id/mask.
For the example below, you can set the mask to the same size/shape as the view you’re masking, and then the ripple will only show for that area. Do something like this:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item android:id="#android:id/mask">
<shape
android:shape="rectangle">
<solid android:color="#000000"/>
<corners
android:radius="25dp"/>
</shape>
</item>
<item android:drawable="#drawable/rounded_corners" />
</ripple>
now you can set it as your android:background

How to have a selectable background together with a certain color background?

I know that you would get a selectable (highlight/ripple) background for a view using this:
android:background="?attr/selectableItemBackground"
But what if I want a certain color background together with the ripple/highlight, what do I do?
(I'm hoping for a few lines of code not involving creating separate drawables which is very inelegant)
try this
<?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="#f816a463"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#f816a463" /> <--your custom color-->
</shape>
</item>
</ripple>
And then
android:background="#drawable/ripple_effect"
in your button

Android button drawing circle effect

Hey guys I just wanna ask question about the default button in android studio when you click it it has effect that it draw circle and fills the button but i wanna know how i can do that in custom button ? thanks for reading
Create a new file "res/drawable/ripple_effect.xml" then add this as background for the button through XML or dynamically.
i.e. android:background="#drawable/ripple_effect"
<?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="#f816a463"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#f816a463" />
</shape>
</item>
</ripple>

How to make custom ripple borderless

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>

Categories

Resources