Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are shortcuts added to Androids widget menu? [duplicate]

Tags:

android

In Nova Launcher (and many other custom ones) there is a Gesture configuration where you can assign various actions to a gesture. Among those actions there is a Shortcuts tab. How can I add a shortcut in my own app that can be recognized by the launcher? (I suppose it's something that I have to add to the Manifest and set an Intent to it)

EDIT: Here's an image to point out those actions.

like image 525
Alex Newman Avatar asked Dec 01 '25 14:12

Alex Newman


1 Answers

These are very poorly documented and unfortunately were previously called "Launcher shortcuts" and now have been left nameless. In addition to being in Nova's gesture menus and similar, they also show next to widgets in the widget drawer. I think of the as "widget shortcuts".

The official documentation seems to be limited to a demo in ApiDemos: https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.java

The idea is that in your manifest you handle android.intent.action.CREATE_SHORTCUT and return a result to the launcher with the label, icon and target intent. Then the launcher can make it into an icon on the desktop or just use the intent for a gesture or whatever.

In your AndroidManifest.xml

<activity android:name=".CreateShortcut">
    <intent-filter>
        <action android:name="android.intent.action.CREATE_SHORTCUT" />
            <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Then when the user picks your shortcut the launcher does a startActivityForResult of that activity, you can either finish right away with a result, or you can display a UI to the user to let them configure the shortcut. Either way, when ready return the info to the launcher:

Intent result = new Intent();
result.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
result.putExtra(Intent.EXTRA_SHORTCUT_NAME, targetLabel);
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
            this,  R.drawable.ic_launcher_shortcut);
result.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

setResult(RESULT_OK, result);
finish();
like image 160
Kevin TeslaCoil Avatar answered Dec 03 '25 23:12

Kevin TeslaCoil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!