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)
Remove shortcut is not working for me in the downloaded source code.
Thanks Sir,
Very good explanation and both install and uninstall working fine.
Thanks Again :)
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.
you widget with a pending intent or a service
Thank you very much.
what to do for creating shortcut at install time ?
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
I have a requirement to create the shortcut at installation stage. How can I do that?
Thank you very much….:)
— Sir, Thanks for your example, but when i clicked on the shortcut, it said “Application not installed”. Can you help me on this.
Nice tutorial but can you plz give me the solution for creating shortcut at installation time.
Thanks in advance.
Thx for tutorial. It was very helpful.
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:
Remove esta, postei abaixo uma corrigida e com menos parametros;
ITs Fine for me ..nice example ..
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
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.
Hi Thanks for the tutorial.
Could you please let me know how to create the shortcut of other installed applications..
Thanks,
Jetti
hi..
how to install application without shortcut?
what must configure in activity?
Thanks,
Sapril
Great tutorial! All your tutorials are very helpful, thank you for helping me with Android :)
Thank you . Very helpful post .
Thanks for such easy & nice tutorial :)
I really appreciate your post and agree with you. Thanks for good information and way of explaining. This will be really useful. thanks for posting such information.
Can i do something for widgets as well.
How to call it by js in chrome browser