Skip to content Skip to sidebar Skip to footer

Install Home Screen Widgets Progrommaticly

I have main activity window in my app, which displays three icons on it, also there are 3 home screen widgets in this application. is there a possibility for long press on one of

Solution 1:

The user will have to manually install the app widget. Since widgets need to be positioned -- and so users do not have app widgets forced upon them -- users must choose to add it to their home screen.


Solution 2:

In Android O, its possible to set app widget programmatically.

AppWidgetManager mAppWidgetManager =
    context.getSystemService(AppWidgetManager.class);

AppWidgetProviderInfo myWidgetProviderInfo = new AppWidgetProviderInfo();
ComponentName myProvider = myWidgetProviderInfo.provider;

if (mAppWidgetManager.isRequestPinAppWidgetSupported()) {
  // Create the PendingIntent object only if your app needs to be notified
  // that the user allowed the widget to be pinned. Note that, if the pinning
  // operation fails, your app isn't notified.
  Intent pinnedWidgetCallbackIntent = new Intent( ... );

  // Configure the intent so that your app's broadcast receiver gets
  // the callback successfully. This callback receives the ID of the
  // newly-pinned widget (EXTRA_APPWIDGET_ID).
  PendingIntent successCallback = PendingIntent.createBroadcast(context, 0,
          pinnedWidgetCallbackIntent);

  mAppWidgetManager.requestPinAppWidget(myProvider, null,
           successCallback.getIntentSender());
}

Also check out Google official documentation


Post a Comment for "Install Home Screen Widgets Progrommaticly"