How to add a widget to the Android home screen from my app?
Asked Answered
N

4

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?

Niobous answered 19/4, 2013 at 8:59 Comment(4)
Possible duplicate of Binding AppWidgets to AppWidgetHost - AndroidCzarist
@Czarist 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: meta.#253429Standush
How can add an App Widget? I need it but don't know how.Whiffet
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 itemMediacy
S
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);
Stoner answered 19/4, 2013 at 9:6 Comment(13)
I've read it in multiple places that it is possible (here on SO) but no one explained how.Niobous
please see my edited answer. .. also in link2 Mark Murphy said it is not possible to automatically place a widget in home....Stoner
As I said I don't want to automatically add widgets but let the user decide to add them.Niobous
@AdamArold If you want the user decide to add the widget, I think StineSpike's answer is correct. Maybe you should accept his answer?Jilleen
Ok. I completely forgot about this question.Niobous
How do you get AppWidgetID and KEY_CODE?Flat
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.Selfjustifying
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?Selfjustifying
Can you "direct" the picker to your widget with an extra with the package or something similar?Selfjustifying
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.Jonathanjonathon
how can i get AppWidgetID in my acticity, im using AppWidgetProvider for creating widget. @StinePikeFructify
@Jonathanjonathon Same with me. So there is no way for a normal app to add widget on screen by showing that chooser dialog, right?Juristic
@ShobhitPuri I gave up on trying this long time ago. I haven't found any way or API to do it.Jonathanjonathon
D
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

Derosa answered 14/6, 2017 at 3:28 Comment(2)
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 allDelaine
@Delaine - It looks like the documentation pages have changed since I posted this answer. I've edited the answer to include a more helpful link.Derosa
S
9

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

Surge answered 28/6, 2022 at 7:20 Comment(1)
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 itemMediacy
D
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.

Drooff answered 19/4, 2013 at 9:6 Comment(1)
I'm trying to write it in a way that the user's interaction is needed to place the widget.Niobous

© 2022 - 2024 — McMap. All rights reserved.