Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Making it Change Color

saikiran maddukuri
saikiran maddukuri
17,919 Points

Color of widget not changing

please help i have made the code same as in the video but differ in variable names and the color is not changing

package com.teamtreehouse.android.listwidget;

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.widget.RemoteViews;

public class AppWidgetTheme extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        //int[] ids=AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context,AppWidgetProvider.class));
        int[] realids=AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context,AppWidgetProvider.class));

        for(int ids : realids){

            Intent intent=new Intent(context,AppWidgetTheme.class);
            intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,realids);

            RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.widget_layout);
            int r = (int)(Math.random() * 0xff);
            int g = (int)(Math.random() * 0xff);
            int b = (int)(Math.random() * 0xff);
            int color = (0xff << 24) + (r << 16) + (g << 8) + b;
            remoteViews.setInt(R.id.colored,"setBackgroundColor",color);

            PendingIntent pendingIntent=PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

            remoteViews.setOnClickPendingIntent(R.id.colored,pendingIntent);
            appWidgetManager.updateAppWidget(ids,remoteViews);
        }
    }
}

layout xml is

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/colored"
    android:background="@android:color/holo_blue_dark"
    >

</FrameLayout>

xml resource file is

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="100dp"
    android:minHeight="100dp"
    android:resizeMode="vertical|horizontal"
    android:updatePeriodMillis="3600000"
    android:initialLayout="@layout/widget_layout"
    android:widgetCategory="home_screen"
    />

Ben Deitch help me