Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add button drawable color programmatically [duplicate]

Tags:

android

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));
like image 291
Vivek Shankar Avatar asked Nov 26 '25 18:11

Vivek Shankar


1 Answers

It works..

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

If you using hexcode

 Deletecomponentbtn.setForeground(Color.parseColor("oxff00ff00"));
 Deletecomponentbtn.setBackground(Color.parseColor("oxff00ff00"));
like image 121
GK_ Avatar answered Nov 28 '25 14:11

GK_



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!