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" />

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);
    }

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" />

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);
    }

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>

The layout looks something like following:
android_add_shortcut_demo

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);
    }
    
}

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>

Screenshots of App

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

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

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

Download Source Code

Android_Add_Shortcut_example.zip (689 KB)



19 Comments

  • Vaibhav 11 December, 2012, 18:26

    Remove shortcut is not working for me in the downloaded source code.

  • Andy 17 February, 2013, 22:26

    Thanks Sir,
    Very good explanation and both install and uninstall working fine.

    Thanks Again :)

  • Akash 20 February, 2013, 22:33

    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.

    • Ramesh 10 June, 2014, 10:43

      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.

  • chud 18 April, 2013, 14:53

    Thank you very much.

  • magic 22 May, 2013, 15:51

    what to do for creating shortcut at install time ?

  • murali 12 July, 2013, 16:55

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

  • hien giang 21 July, 2013, 19:42

    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

  • H Dabis 29 July, 2013, 14:10

    I have a requirement to create the shortcut at installation stage. How can I do that?

  • A Shen 25 January, 2014, 22:04

    Thank you very much….:)

  • Vaibhav katkar 7 March, 2014, 20:24

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

  • Aashish 12 March, 2014, 11:20

    Nice tutorial but can you plz give me the solution for creating shortcut at installation time.
    Thanks in advance.

  • razer 2 April, 2014, 14:02

    Thx for tutorial. It was very helpful.

  • Glayson Junio 10 April, 2014, 20:23

    Criei uma função genérica com base no teu artigo e corrigi um erro da tua função, onde duplicava o atalho, se interessar ai vai:

     
        public static void criarAtalho(Class<?> classe, Application application, int resourceId) {
            //Adding shortcut for MainActivity on Home screen
        	//Permissão: <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
            //geral.criarAtalho( SACMobileActivity.class,
    		//           getApplication(), 
    		//           R.drawable.carrinho);
        	
            String NomeApp = "";           
            PackageInfo pInfo;  
            try {  
                pInfo = application.getPackageManager().getPackageInfo(application.getPackageName(), 0);             
                NomeApp = application.getResources().getString(pInfo.applicationInfo.labelRes);             
            } catch (NameNotFoundException e) {  
                e.printStackTrace();  
            }     	
        	
            Intent shortcutIntent = new Intent(application.getApplicationContext(), classe);
             
            shortcutIntent.setAction(Intent.ACTION_MAIN);
         
            Intent addIntent = new Intent();
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, NomeApp);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(application.getApplicationContext(), resourceId));          
            addIntent.putExtra("duplicate", false);     
            addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            application.getApplicationContext().sendBroadcast(addIntent);
        } 
    
  • Glayson Junio 10 April, 2014, 20:24

    Remove esta, postei abaixo uma corrigida e com menos parametros;

  • Virat Bhavsar 17 April, 2014, 12:33

    ITs Fine for me ..nice example ..

  • Levid 11 June, 2014, 12:41

    when u delete make sure shortcutIntent in Intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent) is the same you put when you add, or it will fail

  • saqibrazzak 5 August, 2014, 12:39

    Android;
    How to create shortcuts of all installing apps from launcher or app drawer????
    i want to make an app in which there is a grid view or listview in which all installed apps exist and after selecting any app shortcut should b appear on Home Screen.
    plz help me.(sorry for my bad english)..Thanks in advanced.

  • jetti 5 August, 2014, 14:34

    Hi Thanks for the tutorial.

    Could you please let me know how to create the shortcut of other installed applications..

    Thanks,
    Jetti

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]

Current ye@r *