Aside from AppWidgets
, Android also has a concept of Launcher Shortcuts that are often grouped under the "Widget" label. That Dropbox folder is a Launcher Shortcut.
Shortcuts are simple, all data (icon, label, intent) is static, determined at creation time. They can't be updated dynamically, or resized, or scroll. But they match the launcher styling (and can be placed inside folders or the dock) and use less resources than AppWidget
s.
Sadly they're poorly documented. You can find a sample in the ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.java ( https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.java ) and references to them at https://developer.android.com/reference/android/content/Intent.html#EXTRA_SHORTCUT_ICON (all the EXTRA_SHORTCUT_... items).
You need an Activity
and AndroidManifest
intent-filter to handle creating the shortcuts:
AndroidManifest.xml
<activity
android:name=".LauncherShortcutActivity" >
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This activity will be called by the launcher with startActivityForResult
, you may present an interface (for example let the user select a folder the shortcut should be to) but ultimately must return an icon, label and intent to the launcher.
void setResult(CharSequence title, int iconResourceId, Intent targetIntent) {
Intent data = new Intent();
data.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, iconResourceId));
data.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
setResult(Activity.RESULT_OK, data);
finish();
}
In this case, the icon is specified as a resource of my app. Alternatively the icon can be a bitmap (for example a contact photo):
data.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);