Have you seen the android app options menu on press any application? Android shortcuts menu deliver specific types of content to your users. In this tutorial, you will learn how to create shortcuts for to android App in different ways.
In this tutorial, you will learn the following:
- App shortcuts overview
- Shortcut types
- Create shortcuts
App shortcuts overview
Shortcuts are used in-app for a purpose to your users by helping them quickly access parts of your app. Like a youtube app has an option on the long-press icon that shows a three-option menu “Trending”, “subscriptions” and “search”. By clicking on the option you can directly access these features.
Android App Shortcut types
- Static shortcuts:- Context doesn’t change
- Dynamic Shortcuts:- Context constantly changes
- Pinned Shortcuts:- Context is defined by the user
Let’s Build a Simple Android App Shortcut on Long press :
In the example, we will cover Static and Dynamic shortcuts. When a user does Long click on the application icon it will popup a menu with app options.
Step 1. Create a new project “Build Your First Android App in Kotlin“
Step 2. Open the “MainActivity.kt” and add the following code.
We added a dynamic shortcut code in the activity.
package com.eyehunts.androidappshortcuts import android.content.Context import android.content.Intent import android.content.pm.ShortcutInfo import android.content.pm.ShortcutManager import android.graphics.drawable.Icon import android.net.Uri import android.support.v7.app.AppCompatActivity import android.os.Bundle import java.util.* class MainActivity : AppCompatActivity() { lateinit var appContext: Context override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) appContext = applicationContext val shortcutManager = getSystemService(ShortcutManager::class.java) val shortcut = ShortcutInfo.Builder(appContext, "id1") .setShortLabel("Website") .setLongLabel("Open the website") .setIcon(Icon.createWithResource(appContext, R.drawable.baseline_open_in_browser_black_18dp)) .setIntent( Intent(Intent.ACTION_VIEW, Uri.parse("https://www.tutorial.eyehunts.com/")) ) .build() shortcutManager!!.dynamicShortcuts = Arrays.asList(shortcut) } }
Step 3. Add the following string in the strings file. (res/values/strings.xml)
Android App Shortcuts Food menu
Step 3. Create a shortcuts.xml file and add the following code.
Now we are creating static shortcuts. You need to create shortcuts file at “res/xml/shortcuts.xml“, if XML directory not there, then create it.
See the below screenshot for better help.
Add the following code in “shortcuts.xml”.
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="compose"
android:enabled="true"
android:icon="@drawable/baseline_restaurant_menu_black_18dp"
android:shortcutShortLabel="@string/compose_shortcut_rest">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.myapplication"
android:targetClass="com.example.myapplication.ComposeActivity" />
<!-- 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>
Step 4. Now Run the application, in an emulator or on your Android device.
Output screenshot Android Shortcuts app example :
Download source code Android Shortcuts in kotlin
https://github.com/EyeHunts/AndroidAppShortcuts
How to display menu options after Long press on an Android app icon like Paytm and Swiggy?
Answer: It can be your interview question. You can do it using shortcuts.
Do comment if you have any doubts and suggestions on this tutorial.
Note: This example (Project) is developed in Android Studio 3.3.2. Tested on Android 9 ( Android-P), compile SDK version API 28: Android 9.0 (Pie)
MinSdkVersion=”25″
TargetSdkVersion=”28″
Coding in Kotlin