开发者

Android widget button custom dialog

Is it possible to open a custom dialog from widget button? I created a widget with a button. With this tutorial if i click ButtonP1 a toast msg appears. But i want a custom dialog to appear. There is also an intent example for the button in the tutorial but for my custom dialog it is not working, however if i want to open an activity with that it is working fine. I want a custom dialog to appear when user clicks on a button on the widget.

Here is this code where Main.class is an activity:

public class HelloWidget extends AppWidgetProvider {

public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    context.startService(new Intent(context, UpdateService.class));

     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetmain);
     Intent configIntent = new Intent(context, Main.class);
     configIntent.setAction(ACTION_WIDGET_CONFIGURE);
     PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
     remoteViews.setOnClickPendingIntent(R.id.ButtonP1, configPendingIntent);
     appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
 }

When i change Main.class to CustomizeDialog.class, nothing happens.

The CustomizeDialog.java looks like this in a nutshell:

public class CustomizeDialog extends Dialog implements OnClickListener {
Activity mActivity;
public CustomizeDialog(Activity activity) {
        super(activity);
         mActivity = activity;
...
}
@Override
    public void onClick(View v) {

        if (v == okButton)
        {
            dismiss();
            }
        if (v == cancelButton)
        {
            dismiss();
        }
    }
}

Edit 06.21.2011

Thank to you guys, i was able to call an activity with framelayout which is almost what i wanted. Howe开发者_JS百科ver, I want to do this with a customdialog, but with the code i am using, it is not appearing when i click on the widget button:

Intent configIntent = new Intent(context, CustomizeDialog.class);
     configIntent.setAction(ACTION_WIDGET_CONFIGURE);
     PendingIntent configPendingIntent = PendingIntent.getActivity(context, REQUEST_CODE_THREE, configIntent, 0);
     remoteViews.setOnClickPendingIntent(R.id.ButtonP3, configPendingIntent);

If i change CustomizeDialog.class to Main.class, which is an activity, it is working fine.

Please find CustomDialog.class's structure above.


Like Tanner already pointed out you should use an Activity for this.

In the manifest you have to add a few attributes to the dialog activity.

android:launchMode="singleInstance" - to place the activity (dialog) at the root of a new task, without this the activity will be placed on top of the main activity in your application.

android:excludeFromRecents="true" - so you can't reach the dialog by long pressing the home-key.

android:theme="@android:style/Theme.Dialog" - style it as a dialog.

<activity android:name=".InputDialog" 
    android:launchMode="singleInstance"
    android:theme="@android:style/Theme.Dialog"
    android:excludeFromRecents="true">
</activity>

In the dialog activity you have to call finish() on the activity after the user clicked on OK, Cancel or the back button. If you don't do this only the dialog will be closed and the activity will remain visible.

I wrote a how-to a while back when I faced the same problem, can be found here on my blog.


One way to do this would be to show the dialog in the activity that is opened by clicking on your widget. If you use something like a FrameLayout you can run an activity that appears to be hovering over the top of your home screen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜