Portal Berita dan Informasi Terbaik di Indonesia

Create App Shortcuts In Android. Quick actions into your application | by Nishant Aanjaney Jalan | Jun, 2022

Quick actions into your application

Preview of our application

We’ve seen shortcuts that appear on WhatsApp, Facebook, Google Chrome, etc on our applications. They are useful when you want quick navigations into the application without going through the long process once instead. Implementing is not difficult at all; we just need to add some configurations that will do the trick.

These are shortcuts that don’t change based on the data of the application. Like opening a new tab on Google Chrome. They will always be present for you on the home screen.

  1. Let’s start by creating an xml-v25 folder under res/; and create a file called shortcuts.xml. You can name this file anything you wish, but the folder names must be exact.
  2. Instead of <resources></resources>, we need to use the <shortcuts></shortcuts> tag instead. All our static shortcuts will be defined here.
  3. Let’s define a basic shortcut by adding a <shortcut/> tag here.

Each shortcut will need a shortcutId and a shortcutShortLabel.

4. Inside this, we need to define an Intent. Intents are ways you can enter the application with the shortcut. For the basic one, let’s simply add an intent to the MainActivity with an extra.

When the user clicks on the shortcut that says “Main”, then an Intent will be sent to the MainActivity with the key content_key and the String extra “Msg from shortcut”

5. Retrieving the extra in MainActivity. We can simply get the extra as we normally do:

val shortcutExtra = intent.getStringExtra("content_key") ?: ""
textView.text = shortcutExtra

6. Don’t forget to register this in your AndroidManifest.xml. You must add the shortcuts configuration within the launcher activity declaration.

Voilà! You have now added a static shortcut to your application

Alongside Static Shortcuts, you may also want to create some Shortcuts whose data is dependent on a network call, for example. In WhatsApp, you have 3 shortcuts to the most recent chats — which are always changing. In this scenario, you would want to specify the shortcut via code.

We are applying the same logic as we did before with the Static shortcuts. However, here you can pass parameters and set a custom label and custom intents based on the action you would want to have.



Source by [author_name]

Leave a Reply

Your email address will not be published.