# Android App Shortcuts

Most popular apps on Android phones have launcher shortcuts; app-specific destinations or features that appear when you long tap their icon in the launcher.

![App Shortcuts](https://cdn.hashnode.com/res/hashnode/image/upload/v1639212926995/qtkJl7-Rd.jpeg)

Users can even drag the shortcuts outside to your home screen as demoed below:
![ezgif-7-ed2aaf7a35c2.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1639214442545/9keSH0uLu.gif)

This has been available since Android Nougat (Android 7.1). Even if your app has a lower minSdkVersion (Around Lollipop or even lower), you can set an attribute `tools:targetApi="n_mr1"` in the shortcuts.xml that we'll define soon.

## Static Shortcuts
In this article, we're gonna look at static shortcuts **only**, which are basically intents that open up different activities in your app. I'll also offer the solution that I used for my fragment-centric app. There are also dynamic shortcuts that apps like WhatsApp and Telegram use which change with time - they won't be covered here. For more information, consider checking the official docs: [App Shortcuts](https://developer.android.com/guide/topics/ui/shortcuts/)

## How to add a static shortcut?
Create an Android Resource Directory under `res` and name it `xml`
![Screenshot_20211211_143638.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1639213638621/ctZoGcDO3.png)

Add a new **XML Resource File** under `xml` with the name shortcuts
![Screenshot_20211211_143810.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1639213845183/_sZpNv7Il.png)

A newly generated shortcuts.xml appears. Replace it with this XML:
```XML
<shortcuts xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="your_id_here"
        android:enabled="true"
        android:icon="your_icon_drawable_here"
        android:shortcutShortLabel="your_string_resource_label_here"
        android:shortcutLongLabel="your_string_resource_long_label_here"
        tools:targetApi="n_mr1">
        <intent
            android:action="some_action_name"
            android:targetPackage="your.package.name" 
            android:targetClass="activity.package.name" />
    </shortcut>
    <!-- More Shortcut Tags -->
</shortcuts>
```

Here is a screenshot of mine:
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1639214980695/cwCktyHRI.png)

Add your shortcut meta-data under the host or target `activity` tag in your `AndroidManifests.xml` like this:
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1639214758359/FrKsq9G62.png)

## Optional: If you're using fragment centric architecture
If you're using a single activity with multiple fragments or similar architecture in your app, just match the intent action attribute inside your host activity with the one you specified in `shortcuts.xml`, like this:
```Kotlin
when (intent.action) {
           // If intent action matches, navigate to that fragment.
            "shortcut_add_transaction" -> navController.navigate(HomeFragmentDirections.actionHomeFragmentToAddTransactionFragment())
}
```

And Ta-Da! You just added an app shortcut! Now users can easily access top destinations in your app, right from the launcher.


