0

I want to change the background of this button:

              <Button
               android:id="@+id/button1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentLeft="true"
               android:layout_alignParentRight="true"
               android:layout_below="@+id/button2"
               android:minHeight="106dp"
               android:text="Button" />

how can i do it?

4 Answers4

0

Make one buttoncolor.xml file and place this into:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#FF0000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#FF0000" />
<item android:color="#ffffff" />

Then in your button attribute:

android:background="@drawable/buttoncolor"

Programmatically:

button.setBackgroundColor(Color.WHITE);
Umit Kaya
  • 5,771
  • 3
  • 38
  • 52
0

Use android: background="" attribute to that button. For example you can have #22CCDD in "" in the above line

Ramakishna Balla
  • 1,020
  • 1
  • 8
  • 12
0

Use android:background="" the value can be color or drawable

Fahriyal Afif
  • 560
  • 3
  • 11
0

use the below as drawble file,let suppose button_bg.xml

<shape android:shape="rectangle">
 <solid android:color="@android:color/transparent"/> --> change any color here accorring  to your requirement,and this will the background of your button.
     <corners android:bottomLeftRadius="8dp"  --> use this if you want to make button corners rounded.
              android:bottomRightRadius="8dp" 
              android:topLeftRadius="8dp" 
              android:topRightRadius="8dp"/>

 <stroke android:width="1dp" --> use this if you want button border.
         android:color="#9c9a9b"/> --> change any color here accorring to your requirement

Here is your button code

Button
           android:id="@+id/button1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentLeft="true"
           android:layout_alignParentRight="true"
           android:layout_below="@+id/button2"
           android:minHeight="106dp"
           android:background="@drawable/button_bg" -- > set the above file like this.
           android:text="Button" />

Or simple you can make a image and put that in your drawable folder and can set as button background like:

android:background="@drawable/YOUR_IMAGE_NAME"
Community
  • 1
  • 1
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59