1

I am unable to add shortcut icon on home screen . i want to do this only using manifest and not by adding any method on activity.

i tired this code in manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.fz.atifandroid">
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <uses-permission
        android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission
        android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/icon_image"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">


    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="orientation|screenSize">
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <!--<activity android:name=".MainActivity"/>-->
</application>
 </manifest>
warzone_fz
  • 472
  • 1
  • 9
  • 24
  • check this out [link](http://stackoverflow.com/questions/6337431/android-create-shortcuts-on-the-home-screen/20536046#20536046) – Maher Nabil Jul 21 '16 at 11:25
  • Already answered in [How to add shortcut to Home screen in android programatically](http://stackoverflow.com/questions/16873256/how-to-add-shortcut-to-home-screen-in-android-programatically) – Shiv Buyya Jul 21 '16 at 11:33

4 Answers4

0

Check the link, as this question is similar to Android create shortcuts on the home screen.

One more way is if you are downloading the app from play store then in play store settings you can tick the add icon to homescreen then it will create the app icon in homescreen.

I hope this helps.

Community
  • 1
  • 1
Ironman
  • 1,330
  • 2
  • 19
  • 40
0

Try this,

in Java,

private void ShortcutIcon(){

Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
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);
}

Don't forget to add permission to manifest.xml

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

Happy coding !!!

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
  • should shortcut icon be same as launcher icon? if yes, my launcher icon is placed in mimp folder instead of drawable folder, should i get shortcut icon resource from mimp folder? – Beeing Jk Sep 08 '17 at 02:13
0

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

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

It is answered here

Community
  • 1
  • 1
Shiv Buyya
  • 3,770
  • 2
  • 30
  • 25
0

Add 2 Line in after public class MainActivity extends AppCompatActivity{

final static public String PREFS_NAME = "PREFS_NAME";
final static private String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED";

and call in after MainActivity onCreate

 private void ShortcutIcon(){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
        if (shortCutWasAlreadyAdded) return;
        Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getResources().getString(R.string.app_name));
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
        editor.commit();
    }
Mr_Moradi
  • 354
  • 5
  • 9