Android Button With Background Color And Ripple Effect
Asked Answered
C

4

8

I have an android button, where I want to give it a background color and both ripple effect

my ripple xml is as below.

<?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 is

 <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 the ripple effect I have to remove background color...Is there a way I can keep both.

Comrade answered 13/5, 2016 at 7:4 Comment(1)
github.com/traex/RippleEffectMedium
S
10

This can be achieve by adding the android:drawable in your ripple.xml.

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

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

Then create a drawable for the button background(/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 the drawable in your ripple.xml:

<?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>
Screeching answered 13/5, 2016 at 7:35 Comment(0)
C
2

You can do something like this in the xml:

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

Change your ripple.xml in the the drawable/ folder 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>

Cullum answered 25/4, 2017 at 6:29 Comment(0)
I
1

create ripple_effect.xml in your drawable folder

<?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>

use this xml as your view's background like below

android:background="@drawable/ripple_effect"

hope this will help ..

Inquiry answered 23/8, 2017 at 12:28 Comment(0)
A
0

Create two different versions of xml file so that it will 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>

drawable-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 shaped drawable in drawable 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 the layout.xml:

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/button_effect"/>
Artis answered 22/1, 2018 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.