title: Android Widget Change Background Color
body: <p>I have been searching for how to make the Background of a widget change color I tried putting an Icon that fit over the activity and change that, but it didn't work.  I tried Creating a service that would change it, but that seemed like over kill and didn't work for me.  Someone said to change it as a drawable.</p>
  
  <p>So here is my code,
  Main Activity</p>
  
  <pre><code>public class MainActivity extends AppWidgetProvider {
  private static int layoutId;
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
      super.onUpdate(context, appWidgetManager, appWidgetIds);
      RemoteViews updateViews;
      for (int i = 0; i &lt; appWidgetIds.length; i++) {
          int appWidgetId = appWidgetIds[i];
  
          RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
          layoutId = R.layout.activity_main;
  
          Random rnd = new Random();
          int bgcolor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
          //updating current widget
          views.setInt(layoutId, "setBackgroundResource", R.color.colorPrimaryDark);
  
          updateViews = new RemoteViews(context.getPackageName(),layoutId);
  
          AppWidgetManager.getInstance(context).updateAppWidget(
                  new ComponentName(context, MainActivity.class), updateViews);
  
          }
      }
  }
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:background="@color/white"
      tools:context="com.example.stephan.mywidget.MainActivity"&gt;
  
     &lt;ImageView
  
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/bgcolor"
         android:scaleType="fitXY"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>AndroidManifest</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.stephan.mywidget"&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
  
          &lt;receiver android:name="MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt;
              &lt;/intent-filter&gt;
              &lt;meta-data android:name="android.appwidget.provider"
                  android:resource="@xml/widget"/&gt;
          &lt;/receiver&gt;
  
  
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Color resources</p>
  
  <pre><code>&lt;resources&gt;
      &lt;color name="colorPrimary"&gt;#3F51B5&lt;/color&gt;
      &lt;color name="colorPrimaryDark"&gt;#303F9F&lt;/color&gt;
      &lt;color name="colorAccent"&gt;#FF4081&lt;/color&gt;
      &lt;color name="white"&gt;#FFFFFF&lt;/color&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>Widget.xml</p>
  
  <pre><code>    &lt;appwidget-provider
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:minWidth="146dp"
      android:minHeight="72dp"
      android:updatePeriodMillis="28800000"
      android:initialLayout="@layout/activity_main"&gt;
  
  &lt;/appwidget-provider&gt;
  </code></pre>
  
  <p>I actually need it to change random colors but it wont even change one color.  How do I get it to change?</p>