The Phimpme Android application comes with a great functionality of capturing moments, editing them, and sharing it with the world using various social media or cloud platforms integrated into the application. Sometimes, the user may want to directly go to a particular section of the application without having to go to the home activity all the time. We have solved this issue using the App shortcuts option which is provided on Android versions greater than 7.0 Nougat. When the user long clicks on the icon of the application, it provides us with multiple options to choose which activity or section we would like to go to as depicted in the screenshot below.
In this post, I will be explaining how we have achieved this functionality in the Phimpme Android application.
In the Phimpme app, we have three main sections named Camera, Gallery and Accounts and we have added these three options in the app shortcut menu. To do this, first of all, we need to add a meta data to the main activity in the AndroidManifest.xml file. To do this, search for the activity which contains “android.intent.action.MAIN” and add the following line of code in the activity tag.
<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
Now after we have configured the manifest file, we need to create shortcuts.xml file in the resources folder which will contain the information about our app shortcuts along with the intent action to go to a particular activity.
After creating the xml file, add the following line of code.
<shortcuts xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
Now after this we have to create a shortcut tag and define the id of our shortcut and should add the icon to it which will be displayed to the user along with the shortcut text as depicted in the screenshot above. This can be done using the following line of code.
android:shortcutId="camera" android:enabled="true" android:icon="@drawable/ic_camera_alt_black_24dp" android:shortcutShortLabel="@string/camera_short" android:shortcutLongLabel="@string/camera_long" android:shortcutDisabledMessage="@string/camera_short"
Now after this, we have to define the action which we want to perform when the user clicks on a particular app shortcut. To do this, create an intent tag and add the following lines of code in it.
<intent android:action="android.media.action.IMAGE_CAPTURE_SECURE" android:targetPackage="org.fossasia.phimpme" android:targetClass="org.fossasia.phimpme.opencamera.Camera.CameraActivity" /> <categories android:name="android.intent.category.DEFAULT" />
The above code sets the action type as Image capture and defines the package name of the application. The above code is used to open the Camera activity of the Phimpme Application from the shortcut menu. In the similar fashion as described above, we have implemented the option to open up the Gallery and Accounts section of our application by just modifying the action name of the shortcut.
This is how we have implemented the App shortcut functionality in the Phimpme app. To get the full source code of the Shortcuts.xml file, please refer to the Phimpme Android repository.
- Android Developer’s Guide – App shortcuts – https://developer.android.com/guide/topics/ui/shortcuts.html
- GitHub – Google Sample Project to depict the usage of app shortcuts – https://github.com/googlesamples/android-AppShortcuts
- Blog – Using app shortcuts in Android 7.0 – http://www.brevitysoftware.com/blog/how-to-use-app-shortcuts-in-android-7-1-nougat/
- GitHub – Phimpme Android repository – https://github.com/fossasia/phimpme-android/