Android Create Shortcuts on the Home Screen

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...:)

How can I add homescreen shortcut for app after installing

if you publish your app in google play store after installing app auto-create shortcut but if you want to handle that Android provides us an intent class com.android.launcher.action.INSTALL_SHORTCUT which can be used to add shortcuts to the 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 the 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 shortcut Intent 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 the 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);
}

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.

android api 26+ create shortcut to app at install on home screen not working

I found a similar question that led me to the answer. It turned out you now need to use a pinned sshortcut. I found an exclent demo on AndroidAthority by Jessica Thornsby dated March 20, 2018 about implementing shortcuts on Nougat and Oreo that put it all together for me.

Please see my reply to a very similar question for my code.

I put a simple app that does nothing but install a shortcut for itself on your homepage on github. It works for versions before and after Android 8. Pre Android 8 uses the sendBroadcast method and after creats a pinned shortcut.

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


Related Topics



Leave a reply



Submit