How to Add Shortcut to Home Screen in Android Programmatically

How to add shortcut to Home screen in android programmatically

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 creates 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");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
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.

Also put this code to avoid multiple shortcuts :

  if(!getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).getBoolean(Utils.IS_ICON_CREATED, false)){
addShortcut();
getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).edit().putBoolean(Utils.IS_ICON_CREATED, true);
}

creating android app shortcut on the home screen

Do like This:

Step 1:

Update your manifest.xml :

  <uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

Step 2:

in your MainActivity.java create addShortcut() method and in it`s block put this code :

private void addShourcut(){
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 , "Convertor");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE ,
Intent.ShortcutIconResource.fromContext(getApplicationContext() , R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate" , false);
getApplicationContext().sendBroadcast(addIntent);

}

Step3:

set onClickListener for your view that be create shortcut :

img_pin = (ImageView) findViewById(R.id.img_pin);
img_pin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

addShourcut();
Toast.makeText(MainActivity.this, "shortcut created !", Toast.LENGTH_SHORT).show();

}
});

This is worked for me ...
happy codinngggg...:)

Shortcut Add to Android Home Screen

If I understand correctly, you want to launch an app from a shortcut created by your app. Then you can do something like that :

public void createShortCut{
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext, R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent("com.whatsapp"));
sendBroadcast(shortcutintent);
}

Check this

Edit :

Here's a best way to do it using ShortcutManagerCompat :

fun createShortCut(){
val str= ""
val uri = Uri.parse("http://instagram.com/_u/"+str)
val instagramIntent = Intent(Intent.ACTION_VIEW,uri)
instagramIntent.setPackage("com.instagram.android")
val icon = IconCompat.createWithResource(this,R.drawable.ic_launcher_background)
val pinShortcutInfo = ShortcutInfoCompat.Builder(this, "shortcutID")
.setIcon(icon)
.setShortLabel("MyShortcut")
.setIntent(instagramIntent)
.build()
ShortcutManagerCompat.requestPinShortcut(this,pinShortcutInfo,null)
}

Android create shortcuts on the home screen

The example code uses undocumented interfaces (permission and intent) to install a shortcut. As "someone" told you, this may not work on all phones, and may break in future Android releases. Don't do it.

The correct way is to listen for a shortcut request from the home screen-- with an intent filter like so in your manifest:

<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Then in the activity that receives the intent, you create an intent for your shortcut and return it as the activity result.

// create shortcut if requested
ShortcutIconResource icon =
Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

Intent intent = new Intent();

Intent launchIntent = new Intent(this,ActivityToLaunch.class);

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

setResult(RESULT_OK, intent);

How to add app's shortcut to the home screen

As far as I know, that's an optional feature of the Market app, not of the apps themselves. By design an application does not receive a broadcast about itself being installed. If that codes works, the soonest you can execute it is the first time the user launches the app. That said:

Do. Not. Automatically. Create. App. Shortcuts.

Ever.

Don't usurp the user's UI design.

How to add application shortcuts programmatically in Android

I have read an article which can help you in adding application Shortcut programmatically on Home Screen.

You can refer the example .

You can also refer the stackoverflow question related to shortcut here .

How to add shortcut of an app to the homescreen in android emulator

You can do it, as you do it on your physical device.
Just long press with left mouse button on the required app in the emulator, wait for a while, you will see the home screen, then release left mouse button.



Related Topics



Leave a reply



Submit