Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - How to programmatically set button color
    primarykey
    data
    text
    <p>I am reading in some data from a REST api and need to generate some buttons based on the information the app receives.</p> <p>Because I need the same buttons in many Activity screens I have extended Button to make a RachelButton and I set it up in the constructor.</p> <pre><code>public RachelButton(Context context, Info info) { super(context); this.info= info; setText(info.getTime()); setTypeface(Typeface.DEFAULT, Typeface.BOLD); int identifier = 0; if(info.isAvailable()){ identifier = getContext().getResources().getIdentifier("drawable/info_button_"+info.getType(), null, getContext().getPackageName()); }else{ identifier = R.drawable.info_button_unavailable; } if(identifier == 0){ Log.e("INFO_BUTTON", "no button for "+info.getType()); } setBackgroundResource(identifier); setTextColor(R.color.info_button_text_color); setOnClickListener(new View.OnClickListener(){ public void onClick(View view) { //do stuff } }); } </code></pre> <p>Then an example of the resource I am using to generate a colored button is this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" &gt; &lt;shape&gt; &lt;gradient android:startColor="@color/button_pressed" android:endColor="@color/button_pressed" android:angle="270" /&gt; &lt;stroke android:width="3dp" android:color="@color/button_pressed" /&gt; &lt;corners android:radius="3dp" /&gt; &lt;padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item android:state_focused="true" &gt; &lt;shape&gt; &lt;gradient android:endColor="@color/info_normal" android:startColor="@color/info_normal" android:angle="270" /&gt; &lt;stroke android:width="3dp" android:color="@color/info_normal" /&gt; &lt;corners android:radius="3dp" /&gt; &lt;padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item&gt; &lt;shape&gt; &lt;gradient android:endColor="@color/info_normal" android:startColor="@color/info_normal" android:angle="270" /&gt; &lt;stroke android:width="3dp" android:color="@color/info_normal" /&gt; &lt;corners android:radius="3dp" /&gt; &lt;padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;/selector&gt; </code></pre> <p>As you can see in the code I am setting the text color and I'm sure that this color exists as a resource (thank you IntelliJ).</p> <p>But setting the text color like this has no effect at all, the text color on the button seems to be a darker shade of the button's background color.</p> <p>If anyone could give me some advice as to what to try next I would be most appreciative.</p>
    singulars
    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