Android button with ripple background

I have an android button where I want to give her a background color and a ripple effect

my ripple xml is as follows.

<?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="#BDA0CB"
    tools:targetApi="lollipop">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="#BDA0CB" />
        </shape>
    </item>
</ripple>

and my button

 <Button android:id="@+id/Button_activity_login_ViewProfile" style="?android:textAppearanceSmall"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_marginTop="16dp" android:text="Save"
        android:textStyle="bold"
        android:layout_marginLeft="@dimen/margin_left"
        android:layout_marginRight="@dimen/margin_right"
        android:textColor="#ffffffff"
        android:fontFamily="sans-serif"
        android:background="#ff7e51c2" />

To give a ripple effect, I need to remove the background color ... Is there a way I can save both.

+4
source share
4 answers

This can be achieved by adding android: drawable to your ripple.xml.

First add this to your color.xml. Suppose # ff7e51c2 is the background color you want for your button.

...
<color name="btn_bg_color">#ff7e51c2</color>
....

Then create a selection for the background of the button (/drawable/btn_bg.xml):

<?xml version="1.0" encoding="utf-8"?><shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="2dp" />
<solid
    android:color="@color/btn_bg_color" />
</shape>

Then use drawable in your ripple.xml file:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:drawable="@drawable/btn_bg"/>
</ripple>
+6
source

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="#1182bc"
    tools:targetApi="lollipop">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="#1182bc" />
        </shape>
    </item>
</ripple>

xml , .

android:background="@drawable/ripple_effect"

, ..

+1

You can do something like this in xml:

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/ripple"/>

Change ripple.xmlin the folder drawable/as follows:

<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
                  android:color="?android:colorControlHighlight">
    <item android:id="@android:id/mask">
        <shape android:shape="oval">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>

0
source

Create two different versions of the xml file so that they work on Pre Lollipop devices.

drawable / button_effect.xml: -

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <solid android:color="#3ab5d6"/>
  <corners android:radius="3dp"/>
</shape>

draw-v21 / button_effect.xml: -

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:tools="http://schemas.android.com/tools"
    android:color="#635a5a"
    xmlns:android="http://schemas.android.com/apk/res/android"
    tools:targetApi="lollipop">
    <item android:drawable="@drawable/round_button" />
</ripple>

Create the form selected in the dropdown folder

drawable / round_button.xml: -

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#3ab5d6"/>
    <corners android:radius="3dp"/>
</shape>

You can do something like this in layout.xml:

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/button_effect"/>
0
source

All Articles