0

I am creating an Analog Clock Widget for learning purposes and would like to know how I can make a settings layout to change the clock dial image. I have the widget done already but I can't seem to get an onclicklstener to bring up the settings layout.

This is what I have:

@Override
public void onDeleted(Context  context, int[] appWidgetIds) 
{super.onDeleted(context, appWidgetIds);
}

@Override
public void onDisabled(Context  context) {
        super.onDisabled(context);
        }

@Override
public void onReceive(Context context, Intent intent)
{
    String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
    {

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

        Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.android.alarmclock", "com.android.alarmclock.AlarmClock"));
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, AlarmClockIntent, 0);
        views.setOnClickPendingIntent(R.id.Widget, pendingIntent);

        AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
    }
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
    int[] appWidgetIds) 
{
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.settings);
        Intent launchActivity = new Intent(context,Widget.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,0, launchActivity, 0);
        updateViews.setOnClickPendingIntent(R.id.Widget, pendingIntent);
        ComponentName myComponentName = new ComponentName(context, Settings.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(myComponentName, updateViews);
}

}

1 Answers1

0

This question was asked again here.

For future visitors:

Put the code that makes sure the PendingIntent is set, in the onUpdate() method. This makes sure that as soon as the widget is put on the homescreen that the PendingIntent is set.

So:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgets) {
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_activity);
    Intent intent = new Intent(context, Info.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.Widget, pendingIntent);

Also:

If you would also like to have the info Activity pop up automatically when first adding the widget to the home screen, you should read this userful piece of information.

Community
  • 1
  • 1
MarchingHome
  • 1,184
  • 9
  • 15