Nadirah Ibtisam
Nadirah Ibtisam

Reputation: 67

How to deal with AppWidgetProvider in android widget for my project?

I'm now developing a simple widget. Its really new for me, I'm not really getting how to use the AppWidgetProvider

My current widget is only displaying image which it will directly link to a website when user click it.

So, my question is, which of these should I use in AppWidgetProvider?

As we know there are 4 of them.

My current code for it is as below

 public class ExampleAppWidgetProvider extends AppWidgetProvider {

    private static ImageView img;

    public static void updateAppWidget(final Context context,
            AppWidgetManager appWidgetManager, int appWidgetId) {

        img = (ImageView) findViewById(R.id.Image);
        img.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                        .parse("http://www.google.com"));
                PendingIntent.getActivity(context, 0, intent, 0);
            }

        });
    }

    private static ImageView findViewById(int image) {
        // TODO Auto-generated method stub
        return null;
    }

}

Can u see any mistake I have made? The problem is that, the image could not link to the website when I click it.

I have created the internet permission in manifest. Please help me.

THE SOLUTION (I managed to run my project using these :) tq friends for helping )

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        for (int i = 0; i < appWidgetIds.length; i++) {
            int appWidgetId = appWidgetIds[i];

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.legoland.com.my"));
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(),
                    R.layout.widget1);
            views.setOnClickPendingIntent(R.id.Image, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }

Upvotes: 2

Views: 1212

Answers (1)

Andy McSherry
Andy McSherry

Reputation: 4725

In app widgets, you need to use RemoteViews.[setOnClickPendingIntent](http://developer.android.com/reference/android/widget/RemoteViews.html#setOnClickPendingIntent(int, android.app.PendingIntent))

public static void updateAppWidget(final Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
    RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.your_layout);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
    v.setOnClickPendingIntent(R.id. Image, PendingIntent.getActivity(context, 0, intent, 0));

    appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
}

Upvotes: 2

Related Questions