0

I have a button that is round shape. What I want to do is to change the button color when pressed. Can someone tell me how to add the codes to the background xml? Thank you.

<shape 
android:shape="rectangle" android:padding="10dp">
<solid android:color="#c0dfba"/>

 <corners
 android:bottomRightRadius="5dp"
 android:bottomLeftRadius="5dp"
 android:topLeftRadius="5dp"
 android:topRightRadius="5dp"/>

</shape>
jajaja
  • 389
  • 1
  • 12
  • 26

3 Answers3

10

Create XML into res/drawable/button.xml.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
    <shape>
        <solid
            android:color="#343434" />
        <stroke
            android:width="1dp"
            android:color="#171717" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>
<item>
    <shape>
        <gradient
            android:startColor="#343434"
            android:endColor="#171717"
            android:angle="270" />
        <stroke
            android:width="1dp"
            android:color="#171717" />
        <corners
            android:radius="4dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
   </item>
</selector>

And set to your Button as Background like

  <Button
    android:id="@+id/btnValidate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/button"
    android:text="SOS Trasition"
    android:textColor="@android:color/white"
    android:textSize="16sp"
    android:textStyle="bold" />

And if you want Change Button Text Color then create XML file at res/color/button_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
      android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
      android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>

And set this layout XML will apply the color list to a View:

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text" />
M D
  • 47,665
  • 9
  • 93
  • 114
0

If you just want to change color of your button then you can also do like this. But this content is only used at runtime.

I know you are asking through XML file but this is very simple and this is just a simple suggestion though.

imageview.setColorFilter(Color.RED);

or also you can pass any color code like

imageView.setColorFilter(0xFFFF3D60, PorterDuff.Mode.MULTIPLY);
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
-3

crate an xml file in drawable and copy the following lines.. be sure to change the colors as you desire.. and set the xml file as background of your button android:background="your xml"

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="**your desired color when pressed**" />
<item android:color=" **default color when not pressed**"  />
</selector>
Wasim Ahmed
  • 358
  • 3
  • 19