Saturday 11 January 2014

Android Application Shortcut Icon on HomeScreen

1 comment
Today almost every Application we installed in our device, they created  shortcut icon on the Home Screen in order to be more interactive with the user. So, the question is how to do it programmatically. Today, we will covered how to add or remove Shortcut Icon on Home Screen.
For this demo, I have created a sample project with name ShortcutIcon.

First Step:-
I have create a simple xml file with having two button on it.
Add Shortcut and Remove Shortcut.
<LinearLayout 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"
    android:orientation="vertical">
    <Button
        android:id="@+id/bAdd_Shortcut"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add Shortcut" />
   <Button
        android:id="@+id/bRemove"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Remove Shortcut" />
</LinearLayout>
--------------------------------------------------------------------------------------------------------------------------------
Second Step:-
In order to install or uninstall Shortcut icon on the HomeScreen. We need to add two permission in androidmanifest.xml
Here is my androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shortcuticon"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
  <uses-permission
        android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission
        android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.arpit.shortcuticon.MainActivity"
            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>
--------------------------------------------------------------------------------------------------------------------------
Third Step:-
Now begin with MainActivity.Class file.

package com.arpit.shortcuticon;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button add = (Button) findViewById(R.id.bAdd_Shortcut);
        add.setOnClickListener(new OnClickListener() {
             @Override
            public void onClick(View v) {
                addShortcutIcon();
            }
        });
        
        Button remove = (Button) findViewById(R.id.bRemove);
        remove.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                removeShortcutIcon();
            }
        });    
    }
   
    // onClick of addShortcutIcon
    private void addShortcutIcon() {
        //shorcutIntent object
        Intent shortcutIntent = new Intent(getApplicationContext(),
                MainActivity.class);
        
        shortcutIntent.setAction(Intent.ACTION_MAIN);
        //shortcutIntent is added with addIntent
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Icon");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                        R.drawable.ic_launcher));

        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");

        // finally broadcast the new Intent
        getApplicationContext().sendBroadcast(addIntent);
    }
    
    private void removeShortcutIcon() {
        
        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, "Icon");

        addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");

        getApplicationContext().sendBroadcast(addIntent);
    }
    
}
------------------------------------------------------------------------------------------------------------------------------

You can also tweak this code little bit and add icon for the first time an application
is created, like this
I have used SharedPreference

Context mContext=MainActivity.this;
SharedPreferences appPreferences;
boolean isAppInstalled = false;

    @Override
    public void onCreate(Bundle savedInstanceState) { appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
isAppInstalled = appPreferences.getBoolean("isAppInstalled",false);
if(isAppInstalled==false){
       // add shortcutIcon code here
}
// finally isAppInstalled should be true.
SharedPreferences.Editor editor = appPreferences.edit();
editor.putBoolean("isAppInstalled", true);
editor.commit();
}
---------------------------------------------------------------------------------------------------------------------------
Result:-
23
first
Catch Me On: CreateAppFaster.com
Read More...