Saturday, October 5, 2013

Android Create shortcut of Application on Home Page | Create App Shortcut on Home Screen on Android Programmatically

Hello Friends,

Today I am going to share very useful code for creating application shortcut on Home-Screen in android. If we upload our application on Google store, google automatically creates shortcut of application, but if we want create our own launcher use broadcast receiver.  Just copy paste below code and enjoy.

I am using shared-preference for checking app first time install, else it will create many shortcut every-time.

Note: don't forget to add permission in your manifest for create shortcut.
 <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />





1)MainActivity.java
package com.manish.home.shortcut;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;

/**
 *
 * @author manish
 *
 */

public class MainActivity extends Activity {
       Context mContext=MainActivity.this;
       SharedPreferences appPreferences;
       boolean isAppInstalled = false;

       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              /**
               * check if application is running first time, only then create shorcut
               */
              appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
              isAppInstalled = appPreferences.getBoolean("isAppInstalled",false);
              if(isAppInstalled==false){
              /**
               * create short code
               */
              Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);
              shortcutIntent.setAction(Intent.ACTION_MAIN);
              Intent intent = new Intent();
              intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
              intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutDemo");
              intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.logo));
              intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
              getApplicationContext().sendBroadcast(intent);
              /**
               * Make preference true
               */
              SharedPreferences.Editor editor = appPreferences.edit();
              editor.putBoolean("isAppInstalled", true);
              editor.commit();
       }
}

}


2)activity_main.xml
<RelativeLayout 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">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Short Cut Demo!"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

3)AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.home.shortcut"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

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

    <application
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.manish.home.shortcut.MainActivity"
            android:exported="true"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Thanks,
Manish

33 comments:

  1. Hi Manish,

    Thanks for posting this . I have been looking for 3 days.

    Finally, I got. Thanks once again. :D

    ReplyDelete
  2. Replies
    1. I think it should automatic remove shortcut on uninstall app. is it?

      Delete
  3. hey manish can i know how to put entries from one database and make it into a checkbox
    example:
    -Enter Name: Manish
    -Click save button

    when viewed it would be on a checkbox

    ReplyDelete
    Replies
    1. Hi Albert, You have very tough requirement. I think here some confusion i think you want checkbox with that data like-
      "Select your option from list-"
      1)Milk
      2)Fruit
      3)etc

      and in-front of all data you need a checkbox right?

      please have a look here may be it will help you-
      http://www.androidhub4you.com/2013/09/android-dynamic-check-box-example-check.html

      Delete
  4. I want to store something I entered into a database and retrieve it into a checkbox
    Enter animal: dog
    Enter animal: cat
    Enter animal: bat

    Then when i clicked a button it would save into database and at the same time would be called into a checkbox

    Checkbox
    ��cat
    ��dog
    ��bat

    when i check both dog and cat it would go to another layout

    ReplyDelete
  5. i dont need fixed array data rather i need it to save everything and put those values all the things i have entered into a checkbox. So what ever you type or enterd will be in the checklist/ checkbox. Thankyou sir i hope you can help me in my project

    ReplyDelete
  6. i havent found the solution yet sir can u help me

    ReplyDelete
    Replies
    1. At-least create demo app from where you can get data and send me your demo app over my email then i can try if it is possible.

      Delete
  7. sir i sent you in ur email manishupacc@gmail.com right? hope u can help new and young students like me

    ReplyDelete
    Replies
    1. I am not getting what you have did in your demo app. please let me know you want checkbox values from sqlite or something else?

      Delete
    2. i want all the name i entered in one edittext be viewed in a checkbox listview

      Delete
    3. Please check your email. I hv sent you demo app hope it will help you...

      Delete
  8. Hello Manish sir, i wants to create shortcut at installation time.Please guide me.

    ReplyDelete
  9. 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.

    ReplyDelete
    Replies
    1. Hi Saqib, I am not sure but it should be possible using package name of any application.

      Delete
    2. i have done but there is one problem,only App package name is showing on homescreen app icon is not showing,android default icon is showing on homescreen with package name after long press on App.

      Delete
    3. Its seems getting launcher code is not working, please check it should work because i have a app lock app and in this app i am getting all the icons perfectlly.

      Delete
    4. Hai sir I want to add grid view items into the listview using long press listener but listview placed in sliding drawer so i dont know how to do pls help me,my project about GoLauncher,Apex Launcher.

      Delete
  10. Thank you for sharing this!!

    ReplyDelete
  11. code is running fine but no shortcut is created in homescreen??

    ReplyDelete
  12. code is running fine but no shortcut is created in homescreen??

    ReplyDelete
  13. Thank you .. it's really helpful .. thank you >.<

    ReplyDelete
  14. Thanks.. But is there any way to create the shortcut without openning the application???

    ReplyDelete
    Replies
    1. You have to call that service to create shortcut so if you can call that without opening app you can create shortcut too.

      Delete
    2. BTW when you will upload your application on play-store shortcut automatically will be create.

      Delete