As we all noticed the shortcuts or options while using the Flipkart app or Amazon app or Youtube app, Google Drive app etc. Shortcut links could be seen by long tapping on the application icon. Like Google Drive app has an option on the long-press icon that shows a three-option menu “Search”, “Scan” and “Upload”. By clicking on the option we can directly access these features.
We all know these could be customised based on the requirement or business logic of the application. In Summary, Shortcuts or menu options are the best way to move to the destination or required UI over the Android application flow. It really give the best experience to the user and saves their time
Here we are discussing the following be by which we can create the shortcut options
1. By the use of a file which is shortcuts.xml under res->xml
2. Another way is to include the code inside the activity so that it could be added using the programming way
Shortcuts implementation could be done with minimal code and very less effort but it makes our application very impressive to the end user and easy to use.
In conclusion, We could say that by the use of shortcuts, we could add quickly access parts of your app
In this tutorial, we will focus on the following:
Shortcut types
Create shortcuts
There are 3 following types of Shortcuts
Static shortcuts:- Context doesn’t change(fix Shortcuts)
Dynamic Shortcuts:- Context constantly changes depending on the need
Pinned Shortcuts:- Context is defined by the user (User Driven shortcuts)
Let’s Build a Simple Android App which demonstrates how to create shortcuts with the Long press on App Icon
private fun addShortCutsUsingCode() {
val shortcutManager = getSystemService<ShortcutManager>(ShortcutManager::class.java)
val shortcut = ShortcutInfo.Builder(applicationContext, "id_linked_in")
.setShortLabel(getString(R.string.linkedin))
.setLongLabel(getString(R.string.open_linkedin))
.setIcon(Icon.createWithResource(applicationContext, R.drawable.ic_linkedin))
.setIntent(
Intent(
Intent.ACTION_VIEW,
Uri.parse(Constants.LINKEDIN_URL))
)
.build()
val shortcut2 = ShortcutInfo.Builder(applicationContext, "id_youtube")
.setShortLabel(getString(R.string.youtube))
.setLongLabel(getString(R.string.open_youtube))
.setIcon(Icon.createWithResource(applicationContext, R.drawable.ic_youtube))
.setIntent(
Intent(
Intent.ACTION_VIEW,
Uri.parse(Constants.YOUTUBE_URL))
)
.build()
shortcutManager!!.dynamicShortcuts = listOf(shortcut, shortcut2)
}
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="play_store"
android:enabled="true"
android:icon="@drawable/ic_play_store"
android:shortcutShortLabel="@string/open_play_store">
<intent
android:action="android.intent.action.VIEW"
android:data="https://play.google.com" />
<!--By the use of targetPackage and target class, any Activity of same app or other app could be opened-->
<!-- If your shortcut is associated with multiple intents, include them
here. The last intent in the list determines what the user sees when
they launch this shortcut. -->
<categories android:name="android.shortcut.conversation" />
</shortcut>
<shortcut
android:shortcutId="maps"
android:enabled="true"
android:icon="@drawable/ic_maps"
android:shortcutShortLabel="@string/open_google_maps">
<intent
android:action="android.intent.action.VIEW"
android:data="geo:0,0?q=37.423156,-122.084917"
android:targetPackage="com.google.android.apps.maps"
android:targetClass="com.google.android.maps.MapsActivity"/>
<!--By the use of targetPackage and target class, any Activity of same app or other app could be opened-->
<!-- If your shortcut is associated with multiple intents, include them
here. The last intent in the list determines what the user sees when
they launch this shortcut. -->
<categories android:name="android.shortcut.conversation" />
</shortcut>
<shortcut
android:shortcutId="hello"
android:enabled="true"
android:icon="@mipmap/ic_launcher"
android:shortcutShortLabel="@string/launch_Hi_page">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="io.gauravtak.androidappshortcut"
android:targetClass="io.gauravtak.androidappshortcut.HiActivity" />
<!-- If your shortcut is associated with multiple intents, include them
here. The last intent in the list determines what the user sees when
they launch this shortcut. -->
<categories android:name="android.shortcut.conversation" />
</shortcut>
<!-- Specify more shortcuts here. -->
</shortcuts>