Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long click on application icon

Tags:

android

For an application I am building I am looking to have the user be able to long click the icon when starting the application. this would then bring up a menu with different options for the user. Is this possible? I have looked at the developer pages and seen ActionMode.Callback interface when discussing menus. Would this be suitable for an application icon rather than inside the application itself?

thanks.

I tried to make it work but I was getting no where, so I decided to try access the different application from within the context menu. The context_home case is not working. Could you please help.

public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info;

    try {

         info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        } catch (ClassCastException e) {

                 Log.e(TAG, "bad menuInfo", e);

                 return false;
    }

    Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);

    switch (item.getItemId()) {

    case R.id.context_open:

    startActivity(new Intent(Intent.ACTION_EDIT, noteUri));

    return true;

    case R.id.context_copy:

    ClipboardManager clipboard = (ClipboardManager)

    getSystemService(Context.CLIPBOARD_SERVICE);

    clipboard.setPrimaryClip(ClipData.newUri(getContentResolver(),"Note",noteUri));

    return true;

    case R.id.context_delete:

    getContentResolver().delete(noteUri,null, null);

    return true;

    case R.id.context_home:

    PackageManager pm = getPackageManager();
    Intent intent = pm.getLaunchIntentForPackage("project,android.project");
    startActivity(intent);
    return true;

    default:
        return super.onContextItemSelected(item);
    }
}
like image 623
Chris Kerr Avatar asked Dec 20 '22 01:12

Chris Kerr


2 Answers

Use App Shortcuts (starting from API level 25).

  • Find your main activity in AndroidManifest.xml
  • Add reference to the resource with defined shortcuts:

    <meta-data android:name="android.app.shortcuts"
               android:resource="@xml/shortcuts" />
    
  • Define shortcuts in referenced file:

    <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
      <shortcut
        android:shortcutId="compose"
        android:enabled="true"
        android:icon="@drawable/compose_icon"
        android:shortcutShortLabel="@string/compose_shortcut_short_label1"
        android:shortcutLongLabel="@string/compose_shortcut_long_label1"
        android:shortcutDisabledMessage="@string/compose_disabled_message1">
        <intent
          android:action="android.intent.action.VIEW"
          android:targetPackage="com.example.myapplication"
          android:targetClass="com.example.myapplication.ComposeActivity" />
        <!-- If your shortcut is associated with multiple intents, include them
             here. The last intent in the list determines what the user sees when
             they launch this shortcut. -->
        <categories android:name="android.shortcut.conversation" />
      </shortcut>
      <!-- Specify more shortcuts here. -->
    </shortcuts>
    
like image 139
ooi Avatar answered Jan 01 '23 20:01

ooi


If your app targets Android 7.1 (API level 25) or higher, you can define shortcuts to specific actions in your app.

Follow this : https://developer.android.com/guide/topics/ui/shortcuts

like image 23
Christian Avatar answered Jan 01 '23 19:01

Christian