title: How to use setBackground with an Android Widget
body: <p>I asked a similar question before but I was too vague in my request.  The code below draws a nice stylized button.  When you click on the button, you can input a number and based on the number, changes the background color using </p>
  
  <p>remoteViews.setInt(R.id.nmcButton, "setBackgroundColor", color);</p>
  
  <p>Unfortunately, when I try to preserve the styling by using </p>
  
  <p>remoteViews.setInt(R.id.nmcButton, "setBackground", color);</p>
  
  <p>the widget won't load.  Is there a way around this?  Is there a way to preserve the styling while changing the background color?</p>
  
  <p>Here are some of the relevant files</p>
  
  <pre><code>    package test.widget;
  
      import android.os.Bundle;
      import android.app.PendingIntent;
      import android.appwidget.AppWidgetManager;
      import android.appwidget.AppWidgetProvider;
      import android.content.ComponentName;
      import android.content.Context;
      import android.content.Intent;
      import android.util.Log;
      import android.widget.RemoteViews;
      import android.graphics.Color;
  
      public class MyWidget extends AppWidgetProvider {
  
          private final static String TEST_ACTIVITY = "test.widget.action.TEST_ACTIVITY";
          private final static int INTENT_NO_REQUEST = 0; /* no requestCode */
          private final static int INTENT_NO_FLAGS = 0; /* code for no Flags */
          private int count = 9;
  
          @Override
          public void onReceive(Context context, Intent intent) {
              super.onReceive(context, intent);
              Bundle b = intent.getExtras();
              if (b != null) {
                  count = b.getInt("nmcCount");
                  callOnUpdate(context);
              }
          }
  
          private void callOnUpdate(Context context) {
              AppWidgetManager appWidgetManager = AppWidgetManager
                      .getInstance(context);
              ComponentName thisAppWidget = new ComponentName(
                      context.getPackageName(), MyWidget.class.getName());
              int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
              onUpdate(context, appWidgetManager, appWidgetIds);
          }
  
          @Override
          public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                  int[] appWidgetIds) {
              super.onUpdate(context, appWidgetManager, appWidgetIds);
              buildUpdate(context, appWidgetManager, appWidgetIds);
          }
  
          private void buildUpdate(Context context,
                  AppWidgetManager appWidgetManager, int[] appWidgetIds) {
              final int N = appWidgetIds.length;
              for (int i = 0; i &lt; N; i++) {
                  int appWidgetId = appWidgetIds[i];
  
                  RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                          R.layout.widget);
  
                  Intent intent = new Intent(TEST_ACTIVITY);
                  PendingIntent pendingIntent = PendingIntent.getActivity(context,
                          INTENT_NO_REQUEST, intent, INTENT_NO_FLAGS);
                  remoteViews.setOnClickPendingIntent(R.id.nmcButton, pendingIntent);
                  remoteViews.setTextViewText(R.id.nmcButton, String.valueOf(count));
  
                  // the code below works, but the button does not have nice styling
                  // int color = (count &gt;= 5) ? Color.GREEN : Color.RED;
                  // remoteViews.setInt(R.id.nmcButton, "setBackgroundColor", color);
  
                  // this code doesn't work, you get "problem loading widget"
                  int color = (count &gt;= 5) ? R.drawable.btn_green
                          : R.drawable.btn_red;
                  remoteViews.setInt(R.id.nmcButton, "setBackground", color);
  
                  appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
              }
          }
      }
  </code></pre>
  
  <p>btn_red.xml</p>
  
  <pre><code>    &lt;?xml version="2.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;solid android:color="#ef4444" /&gt;
  
         &lt;stroke android:width="1dp" android:color="#992f2f" /&gt;
  
         &lt;corners android:radius="3dp" /&gt;
  
         &lt;padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /&gt;
      &lt;/shape&gt;&lt;/item&gt;
          &lt;item&gt;&lt;shape&gt;
         &lt;gradient android:angle="270" android:endColor="#992f2f" android:startColor="#ef4444" /&gt;
  
         &lt;stroke android:width="1dp" android:color="#992f2f" /&gt;
  
         &lt;corners android:radius="3dp" /&gt;
  
         &lt;padding android:bottom="11dp" android:left="10dp" android:right="10dp" android:top="10dp" /&gt;
      &lt;/shape&gt;&lt;/item&gt;
  
      &lt;/selector&gt;
  </code></pre>
  
  <p>styles.xml</p>
  
  <pre><code>    &lt;resources&gt;
          &lt;style name="ButtonText"&gt;
              &lt;item name="android:layout_width"&gt;wrap_content&lt;/item&gt;
              &lt;item name="android:layout_height"&gt;wrap_content&lt;/item&gt;
              &lt;item name="android:gravity"&gt;center&lt;/item&gt;
              &lt;item name="android:layout_margin"&gt;1dp&lt;/item&gt;
              &lt;item name="android:textSize"&gt;10dp&lt;/item&gt;
              &lt;item name="android:textStyle"&gt;normal&lt;/item&gt;
              &lt;item name="android:shadowColor"&gt;#000000&lt;/item&gt;
              &lt;item name="android:shadowDx"&gt;1&lt;/item&gt;
              &lt;item name="android:shadowDy"&gt;1&lt;/item&gt;
              &lt;item name="android:shadowRadius"&gt;2&lt;/item&gt;
              &lt;item name="android:textColor"&gt;#ffffff&lt;/item&gt;  
          &lt;/style&gt;
          &lt;style name="AppTheme" parent="android:Theme.Light" /&gt;
      &lt;/resources&gt;
  </code></pre>