I am trying to implement ripple effect on my button and its working fine but the problem is the color of the button is gone , though i have given the color in the ripple.xml file , it just removed the color and showing the effect only . Also i am only able to user line and oval effect, unable to use ring and rectangle,
please suggest .
ripple.xml is below
<?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">
<shape android:shape="rectangle">
<solid android:color="#A3080C" />
</shape>
</item>
</ripple>
Related
I have an AppCompatSpinner with a dropdown menu custom background with rounded corners. When I tap an option in the AppCompatSpinner, it shows the ripple background however the ripple goes outside the round corners and forms a normal rectangle. How can i make it so that the ripple fit in the background of popup?
This is how it looks now, ripple effect goes out of rounded background:
Im using popupBackground property to set rounded corners background.
MyActivity:
<androidx.appcompat.widget.AppCompatSpinner
popupBackground="#drawable/background_white_corners"
android:spinnerMode="dropdown"
android:dropDownVerticalOffset="50dp"
...
/>
background_white_corners.xml
<?xml version="1.0" encoding="utf-8">
<shape ... android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius ="30dp"/>
</shape>
My item spinner is just a TextView, without any layout.
<?xml version="1.0" encoding="utf-8">
<androidx.appcompat.widget.AppCompatCheckedTextView ...
android:id="#+id/textview_spinner_item"
/>
Try to use the setClipToOutline(true) on the AppCompatSpinner.
You could also do it in the XML with android:clipToOutline but it's available in API 31+ only
You should use ripple tag inside background drawable for ripple effect in this way:
<?xml version="1.0" encoding="utf-8">
<ripple ....>
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius ="30dp"/>
</shape>
</ripple>
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
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>
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>
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.