Humty
Humty

Reputation: 1361

Change the color of button ( appears) in button properties when button has ripple effect

I want to change the color of button. Let's say: Blue. How can we add color of button in ripple.xml file that is actually the properties of button? From the code below the color of button is transparent.

Button in content_main.xml

  <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ripple"
        android:text="Hello World!" />

ripple.xml

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

Edit1

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:color="#16a463"
    tools:targetApi="lollipop">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="#16a463" />
        </shape>
    </item>
</ripple>

Upvotes: 1

Views: 95

Answers (4)

pankaj khedekar
pankaj khedekar

Reputation: 126

if your intention is only to set the background color of the button the try this in onCreate method after initializing the your button

Button yourBtn = (Button) findViewById(R.id.yout_btn_id);
yourBtn.setBackgroundColor(getResources().getColor(R.color.your_color));

and define the your_color value resource in your color.xml file under values folder

Upvotes: 0

sushildlh
sushildlh

Reputation: 9056

Problem is android:id="@android:id/mask" . i think it provide the custom value of android.

try this one .....

      <ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:drawable="@drawable/button_normal" />
</ripple>

output:-

enter image description here

You have to use the Selector for this animation....

anim.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/ripple"/>
    <item android:drawable="@drawable/button_normal"/>
</selector>

and set you default color in button_normal.xml .....

and set anim.xml in your button background .....

NOTE:- Ripple is working on API level 21 and above .......

Upvotes: 1

Cesario
Cesario

Reputation: 75

Based on your question is, you want to change the color, right ? Just change the value of <solid android:color="#f816a463" in ripple.xml

F816A463

first two digits of color F8 means the opacity, if you want to solid color, please remove the first two digits eg : #ff1122

Upvotes: 0

SaravInfern
SaravInfern

Reputation: 3388

ripple_drawable.xml //place this in drawable-v21 folder

<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="@color/your_color" />
            </shape>
        </item>
 </ripple>

Note : ripple effect will only work on android 5.0 and above

Upvotes: 0

Related Questions