Marian Pavel
Marian Pavel

Reputation: 2876

How should I achieve background color of button with ripple effect

I am trying to implement ripple effect in my android application, I imeplemented the ripple effect on button but I don't know how exactly I should change the button color when is not focused/pressed.

my ripple.xml is this:

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

and my button is this:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Login"
    android:id="@+id/login"
    android:layout_below="@+id/password"
    android:layout_centerHorizontal="true"
    android:layout_margin="20dp"
    android:textStyle="bold"
    android:background="@drawable/ripple"
    android:textColor="#FFFFFF"
    android:textSize="18dp"
    android:elevation="2dp" />

I have tryed to create a selector and set the normal state of button with a color and the state_pressed state_focused to the ripple.xml but it didn't work.

One more solution to achieve this is to wrap the button into a Layout with 0 margins and set the layout color but I don't like this solution. Is there a smarter way how to achieve this ?

Upvotes: 1

Views: 252

Answers (1)

dgngulcan
dgngulcan

Reputation: 3149

Remove android:id="@android:id/mask" from your <item> like 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="#f816a463"
    tools:targetApi="lollipop">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#f816a463" />
        </shape>
    </item>
</ripple>

Upvotes: 2

Related Questions