If you are looking for a feature just like iPhone’s 3D Touch than in the language of android we call it App Shortcuts. Introduced in Android Nougat (API Level 25), whenever you tap and hold on to any app icon then it displays some options which can directly take you to some activity. App shortcuts are only supported in Nougat and above.
They are of three types :
- Static shortcuts: These shortcuts are predefined. They come together with your .apk. Their number and actions stay the same until you publish a new version of your app.
- Dynamic shortcuts: These shortcuts are constructed and included in the list of shortcuts on the fly. They can be created based on some usage statistics and can change over time. There is no need to publish a new version of your app, in order to update your dynamic shortcuts.
- Pinned shortcuts: These shortcuts are published at runtime and also use the ShortcutManager API. During runtime, your app can attempt to pin the shortcut, at which time the user receives a confirmation dialog asking their permission to pin the shortcut. The pinned shortcut appears in supported launchers only if the user accepts the pinning request.
To achieve this feature you can go to Android Developer they have provided details steps to add shortcuts in the app. But adding shortcuts is little tricky some of the beginners may find it hard. We will be discussing the easy way of adding them.
Matthias Robbers have provided a library called Shortbread which provides a very easy way to implement app shortcuts. Let’s get started with this awesome library.
First, You need to add the two dependencies in your build.gradle.
1 2 3 |
// App Shortcuts compile 'com.github.matthiasrobbers:shortbread:1.0.1' annotationProcessor 'com.github.matthiasrobbers:shortbread-compiler:1.0.1' |
And to display the shortcuts, call Shortbread.create(Context context) in your Application class.
1 2 3 4 5 6 7 |
public class YourApplication extends Application { @Override public void onCreate() { super.onCreate(); Shortbread.create(this); } } |
Now all you need to do is add the annotations on your activity to start it from the app shortcut.
1 2 3 4 |
@Shortcut(id = "id", icon = R.drawable.your_drawable, shortLabelRes = R.string.your_lable_string, rank = 1, backStack = {YourBackstackActivity.class}) public class YourActivity extends AppCompatActivity { } |
As we can see in the above code segment, we have just added an annotation @Shortcut and assigned some attributes like
- id – Give an id to the attribute
- icon – Icon that will be displayed.
- shortLabelRes – To provide a short label from a resource you can use shortLabel to provide a string.
- rank – It will decide the position of the shortcut.
- backstack – if you don’t add any activity in the backstack then when you directly open a particular activity on back press the application will get closed. In this case, YourBackstackActivity will be called first so that when you press the back button instead of closing the app YourBackstackActivity will be called.
That’s all!!! try adding the shortcuts in your app and you can definitely play with the other attribute of the library to get more awesome results.
The results will look somthing like this
Thank you very much. This is Vedesh Kumar signing off.