Widget launches a click dialog

Is it possible to launch a dialog by clicking a widget or do I need to start an Activity on widget, then click a dialog?

+3
source share
2 answers

I am sure that you have already decided this, but I will write here anyway, if others, like me, land on this answer.

To launch a dialog box by clicking on a widget, you can define an action created as a dialog box and launch it when a widget is clicked.

First of all, add activity to the manifest, call it DialogWidgetActivity

<activity
    android:name=".DialogWidgetActivity"
    android:theme="@android:style/Theme.Dialog"
    ...
/>

Then in your WidgetProvider call him MyWidgetProvidersnap the click of the widget with the launch of the action

public class MyWidgetProvider extends AppWidgetProvider {

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

    final int N = appWidgetIds.length;

    // Perform this loop procedure for each App Widget that belongs to this
    // provider
    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];

        // Create an Intent to launch the activity-dialog
        Intent intent = new Intent(context, DialogWidgetActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        // Get the layout for the App Widget and attach an on-click listener
        // to it
        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget);
        views.setOnClickPendingIntent(R.id.widget_container, pendingIntent);

        // Tell the AppWidgetManager to perform an update on the current app
        // widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

}
}

, widget_container . , id widget_container, android:id="@+id/widget_container", .

, .

Android.

, :)

+10

AlertDialog .

Update:

onclick.

    AlertDialog.Builder dialog = new AlertDialog.Builder(context);
    dialog.setMessage("Message");
    dialog.show();
-1

Source: https://habr.com/ru/post/1794347/


All Articles