How to customize long press app icon item list (ShortcutManager menu dialog) for your Android App?

Опубликовано: 30 Октябрь 2021
на канале: Programmer World
4,245
29

In this video it shows how one can customize the long press menu item list for the Android App.

When a user presses the icon of any App, Android shows up some of the options. This list of options is called shortcut info list and the dialog is shortcut manager.

In this video, it shows how one can customize this list to do certain actions based on the intent. In this video it uses two menu items. First is to open the programmerworld's home page in a browser and the second is to open one of the subpages.

The code simply uses the ShortcutManager system service to do the customization of the long press list.


I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]

Complete source code and other details can be found in the below link:
https://programmerworld.co/android/ho...


However, the main Java code is copied below also for reference:


package com.programmerworld.longpressiconlist;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcutInfo1 = new ShortcutInfo.Builder(this, "ID1")
.setShortLabel("Home Page")
.setLongLabel("Open Home Page")
.setIcon(Icon.createWithResource(this, R.drawable.ic_launcher_background))
.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://programmerworld.co/")))
.build();

ShortcutInfo shortcutInfo2 = new ShortcutInfo.Builder(this, "ID2")
.setShortLabel("Android Page")
.setLongLabel("Open Android Page")
.setIcon(Icon.createWithResource(this, R.drawable.ic_launcher_foreground))
.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://programmerworld.co/android/")))
.build();

List LESS_THAN ShortcutInfo GREATER_THAN shortcutInfoList = new ArrayList LESS_THAN GREATER_THAN();
shortcutInfoList.add(shortcutInfo1);
shortcutInfoList.add(shortcutInfo2);

shortcutManager.setDynamicShortcuts(shortcutInfoList);
}
}