8

I want to open the launcher's widget picker (for example, the one we get when we long press on home screen) from my Activity. What I want to achieve is, I want to take the user to my widget so that there are more chances that he will consider adding it.

Programmatically adding the widget to home screen will be the best case. But, because that is not possible, I want to go as closer as possible to make user add the widget.

I tried the following but that only opens a dialog (not the launcher's) with all the widgets and by selecting one nothing happens.

Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
startActivityForResult(pickIntent, 1234);

How to make this work? Any suggestions on this scenario are much appreciated.

Mangesh
  • 5,491
  • 5
  • 48
  • 71

1 Answers1

4

It's possible to pin widget to homescreen (official doc) on API 26+:

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

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

if (mAppWidgetManager.isRequestPinAppWidgetSupported()) {
   Intent pinnedWidgetCallbackIntent = new Intent( ... );
   PendingIntent successCallback = PendingIntent.createBroadcast(context, 0,pinnedWidgetCallbackIntent);

   mAppWidgetManager.requestPinAppWidget(myProvider, null,
       successCallback.getIntentSender());
}
HeyAlex
  • 1,666
  • 1
  • 13
  • 31
  • Doesn't work on xiaomi. And buggy on some other devices. – Prilaga Aug 23 '21 at 19:44
  • @ HeyAlex In case of, mAppWidgetManager.isRequestPinAppWidgetSupported() returning False, I want to show a user a GIF to shpow them how to add the widget and then on click of that take user to screen where he can see all the widget available of my app. – Vikas Pandey Dec 27 '22 at 08:12
  • @VikasPandey since u're not a launcher app, u can't do this. APPWIDGET_PICK intent can't be used in your case. so there is no intent on this screen :( – HeyAlex Dec 27 '22 at 11:43
  • Hmm, Thanks @ HeyAlex, What is the best I can do for devices for which, Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && appWidgetManager.isRequestPinAppWidgetSupported) is FALSE – Vikas Pandey Dec 28 '22 at 09:28
  • Will it worthful to make my app a launcher for this? – Vikas Pandey Dec 28 '22 at 09:29
  • 1
    @VikasPandey launcher can be only one. so its impossible in ur case. I think best what can do is to help somehow to navigate (like gif as u mention before) – HeyAlex Dec 28 '22 at 11:26