Static Shortcuts in Android 7.1 Nougat

Static Shortcuts in Android 7.1 Nougat

With the new Android version, Nougat 7.1 (API 25), Google gives us some interesting possibilities. One of them is the option to create shortcuts to an application. In this post I will show you how static shortcuts work.

Shortcuts in Android – what do you need to know?

Android shortcuts allow you to automatically transfer user to desired location within your application. Among others, this allows to grant user faster access to frequently used functionalities of the application, saving the user from performing steps necessary to get to a particular activity.

Android shortcuts can be divided into two types: static and dynamic. They are completely different in terms of how we build them. Static shortcuts are created via XML, as opposed to dynamic ones, which can be created in the application code at any point. In today’s post I will focus on static shortcuts.

Creating static shortcuts

To add static shortcuts to an application, as I have already mentioned, you need to create a right XML. This file must be created under the path /res/xml/shortcuts.xml. All static shortcuts are defined there. Below you can see two examples of shortcuts.

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
	<shortcut
    	android:shortcutId="enableShortcut"
    	android:enabled="true"
    	android:icon="@android:drawable/star_on"
    	android:shortcutShortLabel="@string/short_shortcut_label"
    	android:shortcutLongLabel="@string/long_shortcut_label"
    	android:shortcutDisabledMessage="@string/shortcut_disabled">
    	<intent
        	android:action="android.intent.action.MAIN"
        	android:targetPackage="com.example.dominika.shortcuts"
        	android:targetClass="com.example.dominika.shortcuts.MainActivity" />
    	<intent
        	android:action="android.intent.action.VIEW"
        	android:targetPackage="com.example.dominika.shortcuts"
        	android:targetClass="com.example.dominika.shortcuts.ShortcutActivity" />
	</shortcut>


	<shortcut
    	android:shortcutId="disableShortcut"
    	android:enabled="false"
    	android:icon="@android:drawable/star_off"
    	android:shortcutShortLabel="@string/disable_shortcut_label"
    	android:shortcutDisabledMessage="@string/shortcut_disabled">
    	<intent
        	android:action="android.intent.action.VIEW"
        	android:targetPackage="com.example.dominika.shortcuts"
        	android:targetClass="com.example.dominika.shortcuts.ShortcutActivity" />
	</shortcut>
</shortcuts>

At this stage you should pay attention to several basic parameters:

  • flag enabled – defines whether a shortcut is available,
  • icon – defines the icon that appears to the left of the name in the list of shortcuts in the applications, or simply the main icon after isolating our shortcut on the screen,
  • shortcutShortLabel – mandatory, the short name of our shortcut,
  • shortcutLongLabel – optional field, which is visible only if the name fits on the screen,
  • shortcutDisabledMessage – a message that will appear if a user tries to run unavailable shortcut,
  • intent – key elements determining what the shortcut leads to.

Next you should take into account the file created in the manifest. When you open the AndroidManifest.xml, you need to add the following code, still within the main activity:

<meta-data
            	android:name="android.app.shortcuts"
            	android:resource="@xml/shortcuts"/>

In this example, created are two shortcuts that lead to created example ShortcutActivity. The second one is set as not available and leads directly to the new activity. The first one, in turn, is given 2 intents, that will ensure that you create a back stack of the application. It reflects usual start of two activities in a defined order (firstly MainActivity, then ShortcutActivity).

Below you can see the obtained result:

Emulator view in Android Studio - shortcuts

You may notice that if a shortcut is not available, it does not appear in the list of shortcuts. However, each shortcut can be isolated on the screen, then (with shortcut unavailable) the android icon is grayed out, and after clicking on it a user sees TOAST with defined message.

 

Android Nougat provided us with a few novelties. Shortcuts to applications are certainly one of the more interesting ideas. As you can see adding a static shortcut is not complicated. I encourage you to try to create your own shortcuts.

Learn more

5 Reasons Why You Should Build the UI in ConstraintLayout

ConstraintLayout is a popular solution that Android developers use every day. And not without a good reason. If you’re at the beginning of your journey as a software engineer, get familiar with the most important features this layout can offer. It will help you understand the way it works, and Android app development will become easier.

Read more

Bitrise Tests Made Easier: Update JIRA Issues with Build Number and Forget About Delays in QA Testing

When you trigger Bitrise build with changes and forget to tell QA specialists some essential information, there are two likely scenarios. You waste time waiting for the update from the tester, only you don’t know it’s never coming. Or you get so many questions about builds that you can’t keep up. Sounds familiar? If so, let us tell you about the JIRA issue update that keeps workflow in order.

Read more

How to Implement Feature Flags and Gain More Control over Your App?

Every person with software development experience can share a few stories about incorrectly working features. We build our apps out of small pieces, introduce advanced architecture patterns, and yet sometimes one of the elements fails, causing bugs or even system failures. In such cases, feature toggling can save the day! Find out how to implement feature flags, and make your app more stable.

Read more

Project estimation

Let us know what product you want to build and how we can help you.

Why choose us?

Logo Mobile Trends Awards

Mobile Trends Awards 2017

Nomination in
M-COMMERCE category

Logo Legalni bukmacherzy

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2020

Nomination in SPORTS & RECREATION category

18

client reviews

Clutch logo