XML : 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

  <?xml version="1.0" encoding="utf-8"?>  <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. button_bg.xml:

    '

      <padding      android:bottom="0dip"      android:left="0dip"      android:right="0dip"      android:top="0dip" />    
  2. ripple_effet.xml:

    '

      <item android:id="@android:id/mask">        <shape android:shape="rectangle">            <solid android:color="@android:color/holo_green_dark" />            <corners android:radius="10dip" />        </shape>    </item>    

    '

  3. 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.

No comments:

Post a Comment