How to create and remove shortcut for Android application?

In this post, I will be discussing about how to create and remove shortcut for Android applications programmatically.

What is Android Shortcut?

When an application is installed in Android device, shortcut will be created on the home screen which facilitates the Users to open the App just by clicking on it.

How to enable/disable shortcut creation when new app is installed?

You can control the shortcut creation by enabling/disabling this option in Google Play Store application’s settings, see below.

Google_Play_Store_Settings

You now know (or may already know) about enabling/disabling shortcut creation but you may not know how to create and how to remove shortcut through program. Here we are going to discuss about it. 🙂

Before we start developing the application, take a quick look at the video demo of it.

Video Demo

From the video, I believe you got to know about application.

About application

  • It is a simple application which has two buttons, one to create shortcut and the other to remove shortcut for the application.

You can download source code from here if you don’t want to create Application from scratch, otherwise please proceed with below listings.

How to create and remove shortcut programmatically?

To Create Shortcut

It is very simple to create shortcut programmatically, all you need to do is broadcast the intent with necessary information.

Below code snippet will create the shortcut.

Intent shortCutInt = new Intent(getApplicationContext(),
				MainActivity.class);
shortCutInt.setAction(Intent.ACTION_MAIN);
Intent addInt = new Intent();
addInt.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutInt);
addInt.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyAppShortcut");
addInt.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
		Intent.ShortcutIconResource.fromContext(getApplicationContext(),
				R.drawable.ic_launcher));
addInt.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
// Broadcast the created intent
getApplicationContext().sendBroadcast(addInt);

To Remove Shortcut

Removing shortcut is as simple as creating shortcut, all you need to do is broadcast the intent with necessary information.

Below code snippet will remove the shortcut created.

Intent shortcutInt = new Intent(getApplicationContext(),
				MainActivity.class);
shortcutInt.setAction(Intent.ACTION_MAIN);
Intent addInt = new Intent();
addInt.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutInt);
addInt.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyAppShortcut");
addInt.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
// Broadcast the created intent
getApplicationContext().sendBroadcast(addInt);

Okay, enough theory, let’s create application.

Step 1: Create Android Application Project

  • Create new android project [File >> New >> Android Application Project] with project name AndroidShortCutExample
  • Enter package name as ‘com.prgguru.example’ and activity name as ‘MainActivity’
  • Choose Minimum Required SDK, Target SDK and Compile with. Confused on choosing these options? Take a look at Minimum Required SDK – Target SDK – Compile With post.
  • Click Next button and finally click ‘Finish’ to create project

Step 2: Design screen

Open activity_main.xml and replace it with below code.

<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"
 tools:context="com.prgguru.exameple.MainActivity" >

 <Button
 android:id="@+id/buttonAddShortcut"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:onClick="createShortCut"
 android:text="Create Shortcut" />

 <Button
 android:id="@+id/buttonRemoveShortcut"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:onClick="removeShortCut"
 android:text="Remove Shortcut" />

</LinearLayout>

Step 3: MainActivity.java – Main Class

Open MainActivity.java and replace it with below code.

Each line of code is commented well, if you still have any doubt or question discuss it.

package com.prgguru.exameple;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
 
 /**
 * Method gets invoked when 'Create Shortcut' button is clicked
 * 
 * @param v
 */
 public void createShortCut(View v){ 
 Intent shortCutInt = new Intent(getApplicationContext(),
 MainActivity.class);
 shortCutInt.setAction(Intent.ACTION_MAIN);
 Intent addInt = new Intent();
 addInt.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutInt);
 addInt.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyAppShortcut");
 addInt.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
 Intent.ShortcutIconResource.fromContext(getApplicationContext(),
 R.drawable.ic_launcher));
 // Set Install action in Intent
 addInt.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
 // Broadcast the created intent
 getApplicationContext().sendBroadcast(addInt);
 }
 
 /**
 * Method gets invoked when 'Remove Shortcut' button is clicked
 * 
 * @param v
 */
 public void removeShortCut(View v){ 
 Intent shortcutInt = new Intent(getApplicationContext(),
 MainActivity.class);
 shortcutInt.setAction(Intent.ACTION_MAIN);
 Intent addInt = new Intent();
 addInt.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutInt);
 addInt.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyAppShortcut");
 // Set Uninstall action in Intent
 addInt.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
 // Broadcast the created intent
 getApplicationContext().sendBroadcast(addInt);
 }
}

Step 4: Add permission in AndroidManifest.xml

Add install and uninstall shortcut permissions as shown below:

 <!-- Add permission to install Shortcut -->
 <uses-permission 
 android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
 <!-- Add permision to uninstall Shortcut -->
 <uses-permission 
 android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

[pglinkadssmall1]

Demo

That’s all. It’s time to test our code.

Run the application using emulator or device by right clicking on the project >> Run as >> Android applicaiton >> Choose emulator or device.




Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project (How to import android project in eclipse).

Download Source Code

*apk in Android  is the installation file similar to exe in windows.

[pglinkadssmall]

If you feel this article is helpful and interesting please spread a word about it to your friends and colleagues by sharing the article in Facebook or Twitter.

Share

You are always welcome to provide your comments and feedback from comment box.

[pgwriteforus]

[pgfeedback]

Related Articles

Author: Udhay

Share This Post On
468 ad
Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares