简体   繁体   中英

How to add ripple effect and have custom background on button in android?

Please tell me how to achieve both these together, I have gone through many already questions, but they dont specifically deal with my issue.

I want to have ripple effect on my button as well as it should be rounded and have background color. Also I should be able to control ripple effect color.

I tried few things which are listed below.

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

So to achieve rounded shape I gave android:background="@drawable/button_bg" in the <Button> tag. This makes button rounded and also background color is achieved, But Ripple effect is gone.

Second Method: I tried the using 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 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 <Button> tag android:background="@drawable/button_bg"

With this method button just goes white when pressed returns to background color.

Please tell me how to do it.

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

But if there is any other easier way, more answers are welcome

I did it this way :-

  1. android:background="@drawable/ad_action_view_bg" to my view. // Background is a Ripple drawable
  2. Then I set android - foreground to required color.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM