3

Possible Duplicate:
Android - How to programmatically set button color

I have added table row dynamically in program and added button to it but the color of button is not changing. I added a XML file for adding color for button named redbtn, It's working when I add them in activity, but when I add the button style programmatically color is not changing. what can I do.

redbtn.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

 <item android:state_pressed="true">
 <shape>
  <solid android:color="#DF0101" /> 
  <stroke android:width="1dp" android:color="#ef4444" /> 
  <corners android:radius="3dp" /> 
  <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> 
  </shape>
  </item>
 <item>
<shape>
  <gradient android:startColor="#DF0101" android:endColor="#DF0101" android:angle="270" /> 
  <stroke android:width="1dp" android:color="#992f2f" /> 
  <corners android:radius="3dp" /> 
  <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> 
  </shape>
  </item>


</selector>

In layout :

<Button
              android:id="@+id/btn_spinner_user_search_select"
              style="?android:attr/buttonStyleSmall"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginBottom="5dp"
              android:layout_marginLeft="25dp"
              android:layout_marginTop="5dp"
              android:background="@drawable/redbtn"
              android:text="@string/btn_delete_user_search_user" />

In program:

TableRow addcomponentrow=new TableRow(Deleteuser.this);
            addcomponentrow.setId(200);
            addcomponentrow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

            Button Deletecomponentbtn=new Button(Deleteuser.this);
            Deletecomponentbtn.setText("Delete");
            Deletecomponentbtn.setId(200);
            Deletecomponentbtn.setPadding(10, 0, 20, 2);
            Deletecomponentbtn.setBackgroundColor(R.drawable.redbtn);
            addcomponentrow.addView(Deletecomponentbtn);

            userdetailTable.addView(addcomponentrow,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Cœur
  • 37,241
  • 25
  • 195
  • 267
Vivek Shankar
  • 691
  • 3
  • 14
  • 48

6 Answers6

1

It works..

Deletecomponentbtn.setForeground(Color.GREEN);
Deletecomponentbtn.setBackground(Color.GREEN);

If you using hexcode

 Deletecomponentbtn.setForeground(Color.parseColor("oxff00ff00"));
 Deletecomponentbtn.setBackground(Color.parseColor("oxff00ff00"));
GK_
  • 1,212
  • 1
  • 12
  • 26
0

Try this (If you are adding picture to the background):

  Deletecomponentbtn.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.redbtn));
Skynet
  • 7,820
  • 5
  • 44
  • 80
0

if you want to try then you can give color programatically in and hexcode as mention below.

Deletecomponentbtn.setBackgroundColor(Color.parseColor("HexCode"));

(vatsalshah.co.in)

Vatsal Shah
  • 42
  • 1
  • 6
0

setBackgroundColor takes color value (argb, int), R.drawable.redbtn is resource ID (int) It's never gonna work.

If you want to apply background from resources and you have it's ID, you should use setBackgroundResource(int resid)

Leonidos
  • 10,482
  • 2
  • 28
  • 37
0

Use this,

Deletecomponentbtn.setBackgroundResource(R.drawable.redbtn);

Instead of,

Deletecomponentbtn.setBackgroundColor(R.drawable.redbtn);

Thanks.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
0

use

Deletecomponentbtn.setBackgroundResource(R.drawable.redbtn);

and can correct your redbtn.xml with different states of button.

kumar
  • 691
  • 1
  • 7
  • 16