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 create application shortcut on Home-Screen in android.
If we upload our application on Google store, google automatic create 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!

17 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