Add Ripple Effect To My Button With Button Background Color?


Answer :

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.


Add the "?attr/selectableItemBackground" to your view's android:foreground attribute if it already has a background along with android:clickable="true".


Add Ripple Effect/Animation to a Android Button

Just replace your button background attribute with android:background="?attr/selectableItemBackground" and your code looks like this.

      <Button         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:background="?attr/selectableItemBackground"         android:text="New Button" /> 

Another Way to Add Ripple Effect/Animation to an Android Button

Using this method, you can customize ripple effect color. First, you have to create a xml file in your drawable resource directory. Create a ripple_effect.xml file and add following code. res/drawable/ripple_effect.xml

<?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> 

And set background of button to above drawable resource file

<Button     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="@drawable/ripple_effect"     android:padding="16dp"     android:text="New Button" /> 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?