Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add button drawable color programatticaly
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/3310603/android-how-to-programmatically-set-button-color">Android - How to programmatically set button color</a> </p> </blockquote> <p>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, Its working when I add them in activity,but when I add the button style programatically color is not changing.what can I do. </p> <p>redbtn.xml</p> <pre><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;item android:state_pressed="true"&gt; &lt;shape&gt; &lt;solid android:color="#DF0101" /&gt; &lt;stroke android:width="1dp" android:color="#ef4444" /&gt; &lt;corners android:radius="3dp" /&gt; &lt;padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item&gt; &lt;shape&gt; &lt;gradient android:startColor="#DF0101" android:endColor="#DF0101" android:angle="270" /&gt; &lt;stroke android:width="1dp" android:color="#992f2f" /&gt; &lt;corners android:radius="3dp" /&gt; &lt;padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;/selector&gt; </code></pre> <p>In layout :</p> <pre><code>&lt;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" /&gt; </code></pre> <p>In program:</p> <pre><code>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)); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload