How to add ripple effect and custom background on a button in Android?

Please tell me how to reach both together, I have already considered many questions already asked, but they do not concern my problem.

I want to have a ripple effect on my button, as well as round it and have a background color. I must also be able to control the color of the ripple effect.

I tried a few things that are listed below.

First method: I tried to set style="@style/AppTheme"to a tag <Button>. This gives me a ripple effect and background color (using style <item name="colorButtonNormal">#500347</item>in style), but the button is not rounded.

To achieve a rounded shape, I gave android:background="@drawable/button_bg"in the tag <Button>. This makes the button rounded and the background color is achieved, but the ripple effect has disappeared.

Second method: I tried using a selector. 1. Created button_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:state_focused="true"
        android:drawable="@drawable/button_bg"/>

    <item
        android:state_pressed="true"
        android:drawable="@drawable/ripple_effect"/>

    <item 
        android:drawable="@drawable/ripple_effect"/>

</selector>
  1. rounded background for the button in button_bg.xml:

      <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="#500347" />
    
    <corners
        android:radius="10dip" />
    
    <padding
        android:bottom="0dip"
        android:left="0dip"
        android:right="0dip"
        android:top="0dip" />
    

  2. ripple_effet.xml:

        <ripple xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
      android:color="@android:color/holo_green_dark"
       tools:targetApi="lollipop">
    
    
        <item android:id="@android:id/mask">
    
    <shape android:shape="rectangle">
    
        <solid android:color="@android:color/holo_green_dark" />
    
        <corners android:radius="10dip" />
    
    </shape>
    

  1. In tag <Button> android:background="@drawable/button_bg"

In this case, the method button is simply white when pressing returns to the background color.

Please tell me how to do this.

+4
source share
2 answers

Found my answer here. https://github.com/traex/RippleEffect/blob/master/README.md

But if there is another easier way, more answers are welcome.

+1
source

I did it like this: -

  • android:background="@drawable/ad_action_view_bg" in my opinion.// Background is Ripple drawable
  • Then I set android - foregroundto the desired color.
0
source

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


All Articles