Android Install/Uninstall Shortcut example

Nowadays many android apps installs a shortcut on home screen when you install the app and run it for the first time. This is a nice strategy to engage user by compiling them to use your app. Most of times when a user install an app, the app is deep buried in list of apps making it almost difficult to discover. So its always a good idea to make a shortcut right on home screen. Android comes with simple yet undocumented API to add and remove shortcuts of any app on home screen. Let us check how to do this.

1. Install Shortcut on Home screen

Android provide us an intent class com.android.launcher.action.INSTALL_SHORTCUT which can be used to add shortcuts to home screen. In following code snippet we create a shortcut of activity MainActivity with the name HelloWorldShortcut. First we need to add permission INSTALL_SHORTCUT to android manifest xml.

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Code language: HTML, XML (xml)

The addShortcut() method create a new shortcut on Home screen.

private void addShortcut() { //Adding shortcut for MainActivity //on Home screen Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); shortcutIntent.setAction(Intent.ACTION_MAIN); Intent addIntent = new Intent(); addIntent .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut"); addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher)); addIntent .setAction("com.android.launcher.action.INSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(addIntent); }
Code language: Java (java)

Note how we create shortcutIntent object which holds our target activity. This intent object is added into another intent as EXTRA_SHORTCUT_INTENT. Finally we broadcast the new intent. This adds a shortcut with name mentioned as EXTRA_SHORTCUT_NAME and icon defined by EXTRA_SHORTCUT_ICON_RESOURCE. 

Note: One thing worth noting here is when you define your activity that is invoked from shortcut, you must define android:exported=”true” attribute in <activity> tag. You’ll see this in our demo.

2. Uninstall Shortcut from Home screen

Similar to install, uninstalling or removing shortcut in Android uses an Intent (UNINSTALL_SHORTCUT) to perform the task. In following code we remove the shortcut added on home screen. Again we need a permission to perform uninstall shortcut task. Add following permission to Android manifest xml.

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
Code language: HTML, XML (xml)

The removeShortcut() method does exactly reverse of addShortcut(). Most of the code is similar except removeShortcut calls UNINSTALL_SHORTCUT intent.

private void removeShortcut() { //Deleting shortcut for MainActivity //on Home screen Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); shortcutIntent.setAction(Intent.ACTION_MAIN); Intent addIntent = new Intent(); addIntent .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut"); addIntent .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(addIntent); }
Code language: Java (java)

Let us check the full functionality by creating a demo application.

3. Demo Application

The demo application is very simple. The user interface has two buttons: one to add shortcut to home screen and another to remove it. Following is the simple layout consiting of two buttons.

layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/buttonAddShortcut" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Add Shortcut" /> <Button android:id="@+id/buttonRemoveShortcut" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Remove Shortcut" /> </LinearLayout>
Code language: HTML, XML (xml)

The layout looks something like following:

The MainAcitivity is the activity class that handles user interface events. We add OnClickListener event handlers to both Add and Remove shortcut buttons. Each calls appropriate method to perform the task.

MainActivity.java

package net.viralpatel.android; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Add listener to add shortcut button Button add = (Button) findViewById(R.id.buttonAddShortcut); add.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { addShortcut(); //Add shortcut on Home screen } }); //Add listener to remove shortcut button Button remove = (Button) findViewById(R.id.buttonRemoveShortcut); remove.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { removeShortcut(); //Remove shortcut from Home screen } }); } private void addShortcut() { //Adding shortcut for MainActivity //on Home screen Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); shortcutIntent.setAction(Intent.ACTION_MAIN); Intent addIntent = new Intent(); addIntent .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut"); addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher)); addIntent .setAction("com.android.launcher.action.INSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(addIntent); } private void removeShortcut() { //Deleting shortcut for MainActivity //on Home screen Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); shortcutIntent.setAction(Intent.ACTION_MAIN); Intent addIntent = new Intent(); addIntent .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut"); addIntent .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(addIntent); } }
Code language: Java (java)

Also following is the Android manifest file for reference.

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.viralpatel.android" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Code language: HTML, XML (xml)

Screenshots of App

On clicking Add Shortcut button a toast message appears saying the shortcut has been successfully added on home screen.

If you check the home screen, you will find the shortcut present.

The remove shortcut button triggers UNINSTALL_SHORTCUT intent and removes the shortcut from home screen. It also prints a toast message with confirmation.

Download Source Code

Android_Add_Shortcut_example.zip (689 KB)

View Comments

  • First of all thanks for your excellent tutorial. I am a beginner in the field of Android
    I want a different thing.
    "After pressing the shortcut how i can start another activity instead of default activity"
    how i can do that ???
    please help me.

    • Hi, I want to start a service when click on shortcut icon.I dont want to open app. how can I do this.
      *my approach:
      I have used an activity to start a service but it launches main activity also, I dont knw why.
      it would be better if u can mail me the solution.
      please help asap.

  • very nice example. so simple. worked good for me.. can you please share code for setting Alarm !

  • Hi!
    First, Thanks you for your articles!
    In your write example code, You used button to call install shortcut and remove shortcut. But, when you install app, not automatic create shortcut.
    If you insert code in MainActivity, so shortcut will create in every time open app. app will create more shortcut.
    Can you help me. In case, shortcut automatic create when we install app.
    Thanks you

  • -- Sir, Thanks for your example, but when i clicked on the shortcut, it said "Application not installed". Can you help me on this.

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago