Android Add/Remove Shortcut icon in Home Screen – 2017

Show App Shortcut icon in Android Home screen

Its easy to show app icon in your android home screen directly from your application. You can also remove this icon just taping remove icon button from your app. You can choose any Activity which included in your application. We found its easy to show Android Add/Remove Shortcut icon in android home screen.

android shortcut icon

Below code for MainActivity just copy and paste it –

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.addShortcut);
        add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                addShortcutIcon();
            }
        });

        Button remove = (Button) findViewById(R.id.removeShortcut);
        remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                removeShortcutIcon();
            }
        });
    }

    private void addShortcutIcon() {

//Code Snippet to Add shortcut Icon.
        Intent shortcutAdd = new Intent(getApplicationContext(), MainActivity.class);
        //Instead of MainActivity you can add any activity from you application.
        shortcutAdd.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutAdd);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.shortcut));
        //Above Line is for your application Shortcut Icon.
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }

    private void removeShortcutIcon() {
//Code Snippet to Remove shortcut Icon.
        Intent shortcutRem = new Intent(getApplicationContext(), MainActivity.class);
        shortcutRem.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutRem);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut");

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

Below code for activity_main.xml -

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="#ffd6d9"
    tools:context="write your application package name.MainActivity">

    <Button
        android:id="@+id/addShortcut"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:text="Add Shortcut"
        android:textSize="20sp"
        android:textStyle="bold"
        android:background="#fceb05"
        android:layout_marginBottom="26dp"
        android:layout_above="@+id/removeShortcut"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/removeShortcut"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:text="Remove Shortcut"
        android:textStyle="bold"
        android:textSize="20sp"
        android:background="#f53241"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Now copy and paste the below line in your AndroidManifest.xml

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

Where to paste how is this look like -

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="utube.attraction.com.nextgenearn.appshortcut">

    <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="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Add a Comment

Your email address will not be published. Required fields are marked *