Android : Create shortcut on home screen programmatically

Android : Create shortcut on home screen programmatically

When we installing the android application from the Market Place and run it for the first time, we will see shortcut icon of the application in our home screen. In this post, how to create shortcut on home screen when you install and run the android application at first time.

1. Add permission in manifest xml

For creating the Shortcut icon on the Home Screen. We need to add permission  “”  in AndroidMenifest.xml

<uses-permission android:name="" />

2. Creating shortcut in MainActivity class

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class MainActivity extends Activity {
    // To check if app to be run first time using Preference.
    SharedPreferences appPreferences;
    boolean isFirstRun = false; 
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        // Get preference value to check the app run first time. 
        appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        isFirstRun = appPreferences.getBoolean("isFirstRun",false); 

        if (isFirstRun==false) {
            // Create an explict intent it will be used to call Our application by click on the short cut
            Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);

            // Create an implicit intent and assign Shortcut Application Name, Icon
            Intent intent = new Intent();
            intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Tricing");
            /* If shortcut name is equal to the applicaion name then use
            intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
            intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.logo));
            intent.putExtra("duplicate", false); //Avoid duplicate

            // Set preference  as true
            SharedPreferences.Editor editor = appPreferences.edit();
            editor.putBoolean("isFirstRun", true);
In MainActivity class perform below steps:
  • At first, create preference value ‘isFirstRun’  to check that application run first time.
  • If yes then create an explicit Intent pointing to our application. when the user click on shortcut icon and it will launch our application. Create an implicit intent for creating shortcut on Home Screen. Set application name with help of EXTRA_SHORTCUT_NAME. Set application icon with help of EXTRA_SHORTCUT_ICON_RESOURCE.
  • Finally, change the preference value as true.


The Sample Output Screen:

Android : Create shortcut on home screen programmatically

Leave a Reply