21

I'm writing an application which should be able to add widgets (just text boxes) to the home screen of the user's phone when the user instructs my app to do so. How can I do such a thing?

I know that I can add an app widget but how about adding more?

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
  • Possible duplicate of [Binding AppWidgets to AppWidgetHost - Android](http://stackoverflow.com/questions/4258579/binding-appwidgets-to-appwidgethost-android) – rds Jun 30 '16 at 17:46
  • @rds It looks like this should actually be the dupe target for the other one. This one has more useful answers, and has been viewed about twice as many times as the other one. See here: http://meta.stackoverflow.com/questions/252929/which-question-is-the-better-reference-for-a-duplicate – Daniel Nugent Jun 30 '16 at 23:27
  • How can add **an App Widget**? I need it but don't know how. – Mir-Ismaili Dec 26 '16 at 15:46
  • I want to take users to Select Widget screen, the one we get by long pressing the launcher screen and selecting Widgets, if appWidgetManager.isRequestPinAppWidgetSupported() return sfalse. I have used - Intent(AppWidgetManager.ACTION_APPWIDGET_PICK), but it opens up a dialog with all the widget with title Select a Widget, but nothings happens after clicking on an widget item – Vikas Pandey Dec 28 '22 at 01:21

4 Answers4

31

It is not possible from a app to place a widget in the home screen. Only the home screen can add app widgets to the home screen.

similar links link1, link2, link3

But you can offer user to pick widget from widgetpicker.

    Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
    pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetID);
    startActivityForResult(pickIntent, KEY_CODE);
Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • I've read it in multiple places that it is possible (here on SO) but no one explained how. – Adam Arold Apr 19 '13 at 09:12
  • please see my edited answer. .. also in link2 Mark Murphy said it is not possible to automatically place a widget in home.... – stinepike Apr 19 '13 at 09:18
  • As I said I don't want to automatically add widgets but let the user decide to add them. – Adam Arold May 05 '13 at 14:25
  • 1
    @AdamArold If you want the user decide to add the widget, I think StineSpike's answer is correct. Maybe you should accept his answer? – Jose_GD Aug 23 '13 at 13:57
  • 2
    Ok. I completely forgot about this question. – Adam Arold Aug 23 '13 at 14:03
  • 3
    How do you get AppWidgetID and KEY_CODE? – StackOverflowed Sep 18 '13 at 12:02
  • 1
    KEY_CODE should "Request Code", it's just a unique integer you use to identify your request and it will be returned in the onActivityResult() call. – Andrew Mackenzie Nov 24 '13 at 13:01
  • I have the same question about appWidgetId though. I see you can allocate one from the AppWidgetHost using AppWidgetHost.allocateAppWidgetId(), but how do I get the AppWidgetHost? – Andrew Mackenzie Nov 24 '13 at 13:02
  • 3
    Can you "direct" the picker to your widget with an extra with the package or something similar? – Andrew Mackenzie Nov 24 '13 at 13:03
  • 5
    The picker does not work for me. It appears but when you are picking nothing happens I tried a couple of things to `EXTRA_APPWIDGET_ID` but with no luck. I guess that my problem is that my app is not a launcher but a regular app with a widget. – madlymad Aug 21 '15 at 14:57
  • 1
    how can i get AppWidgetID in my acticity, im using AppWidgetProvider for creating widget. @StinePike – RaRa Feb 05 '16 at 12:11
  • 1
    @madlymad Same with me. So there is no way for a normal app to add widget on screen by showing that chooser dialog, right? – Shobhit Puri Aug 16 '16 at 21:01
  • @ShobhitPuri I gave up on trying this long time ago. I haven't found any way or API to do it. – madlymad Aug 22 '16 at 08:46
16

This was answered a long time ago, but in case anyone stumbles upon this question I thought I should provide an up-to-date answer.

As of Android O (API 26), you can now pin widgets to the user's launcher from your app!

Simply create the widget in your app's AndroidManifest file and use AppWidgetManager to request that the widget be pinned to the launcher. Note that it is up to the launcher to support this feature, so you must call AppWidgetManager's isRequestPinAppWidgetSupported() method before requesting to pin it.

Here's some documentation from Google that goes into more detail: https://developer.android.com/preview/features/pinning-shortcuts-widgets.html#widgets

Hope this helps!

Edit: It looks like the documentation pages have changed since I posted this answer. Here is a more helpful link that gives a code example of how to pin a widget to a launcher: https://developer.android.com/guide/topics/appwidgets/#Pinning

DalvikDroid
  • 523
  • 6
  • 14
  • Well i read it, but I dont think that is what OP was asking for. He wants to programatically add widget on home screen. Your link only describes adding shortcuts - which is basically ICON on home screen doing specific action (which can be also done from within app). But I could not find any mentioning of actually adding widget on home screen at all – qkx Dec 24 '18 at 07:59
  • @qkx - It looks like the documentation pages have changed since I posted this answer. I've edited the answer to include a more helpful link. – DalvikDroid Jan 11 '19 at 16:08
7

Looks like Dalvik Droid's links are updated again, the newest link is at requestPinAppWidget

Example: in MainActivity.java:

private void requestToPinWidget(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            AppWidgetManager appWidgetManager =
                    getSystemService(AppWidgetManager.class);
            ComponentName myProvider =
                    new ComponentName(this, AppWidget.class);
            assert appWidgetManager != null;
            if (appWidgetManager.isRequestPinAppWidgetSupported()) {
                Intent pinnedWidgetCallbackIntent = new Intent(this, MainActivity.class);
                PendingIntent successCallback = PendingIntent.getBroadcast(this, 0,
                        pinnedWidgetCallbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                appWidgetManager.requestPinAppWidget(myProvider, null, successCallback);
            }
        }
    }

Any anywhere in you code you have call requestToPinWidget() to prompt to see if the user wants to add it to the desktop.

Only thing is that this will not add to user's home screen, it will be appended to the page where you see all the apps:

Demo for Android Widget Prompt

gazcn007
  • 141
  • 2
  • 5
  • I want to take users to Select Widget screen, the one we get by long pressing the launcher screen and selecting Widgets, if appWidgetManager.isRequestPinAppWidgetSupported() return sfalse. I have used - Intent(AppWidgetManager.ACTION_APPWIDGET_PICK), but it opens up a dialog with all the widget with title Select a Widget, but nothings happens after clicking on an widget item – Vikas Pandey Dec 28 '22 at 01:20
2

AFAIK default launcher app does not support this. The reason is that user should place everything on the home screen himself. Allowing to place widgets from an application would open doors for apps to "spam" user's home with their "useful" widgets.

Juozas Kontvainis
  • 9,461
  • 6
  • 55
  • 66