How to set the background image of a ripple button in android

My Xml Code:

<Button android:id="@+id/link_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/google_btn" /> 

I apply the default print ripple effect

 <Button android:id="@+id/link_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" /> 

but I need the background "@drawable/google_btn" with "?android:attr/selectableItemBackground" . this means i need a ripple effect with a custom background.

+7
source share
3 answers

In your drawable-v21 you can write code for the ripple effect as you wish. Create an xml file shortcut and set the ripple start tag. Like this:

 <?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorAccentDark"> <item> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@color/button_accent_dark" android:state_checked="false"/> <item android:drawable="@color/button_accent" android:state_checked="true" /> <item android:drawable="@color/button_accent_dark" /> </selector> </item> </ripple> 
+6
source
 <?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="@drawable/btn_rectangle"/> <item android:drawable="@drawable/btn_rect_states"/> </ripple> 

Try the above code here in drawables to get your own custom background that you need.

+5
source

When a button has a background from a painted object, we can add a ripple effect to the foreground parameter. Check below code if it works for my button with a different background

 <Button android:layout_width="wrap_content" android:layout_height="40dp" android:gravity="center" android:layout_centerHorizontal="true" android:background="@drawable/shape_login_button" android:foreground="?attr/selectableItemBackgroundBorderless" android:clickable="true" android:text="@string/action_button_login" /> 

Add below parameter for ripple effect.

 android:foreground="?attr/selectableItemBackgroundBorderless" android:clickable="true" android:focusable="true" 
+5
source

Source: https://habr.com/ru/post/1257367/


All Articles