Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Context Menu Icon in android

I have a Listview with a ContextMenu, but when I setIcon for ContextMenu look like it doesn't work

public void onCreateContextMenu(ContextMenu menu , View v, 
        ContextMenuInfo menuInfo){

    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.context_menu_favorite)
        .setIcon(android.R.drawable.btn_star);      
}
like image 251
Dennie Avatar asked Aug 07 '09 15:08

Dennie


People also ask

Where is context menu in android?

Android context menu appears when user press long click on the element. It is also known as floating menu. It affects the selected content while doing action on it. It doesn't support item shortcuts and icons.

How do you create a context menu?

Open the app -> Java -> Package -> Mainactivity.In this step, add the code to show the ContextMenu. Whenever the app will strat make a long click on a text and display the number of options to select of them for specific purposes. Comments are added inside the code to understand the code in more detail.

How do I add a context menu to a list view?

MainActivity.registerForContextMenu(listView); After a long press on the particular item and selecting the option we can return selected value and process it. Adding Listview and text view to the xml file. You can also customize the listview with icons and images based on your android context menu requirement.

How do I show the pop up menu on android?

Go to app > res > right-click > New > Android Resource Directory and give Directory name and Resource type as menu. Now, we will create a popup_menu file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu.


2 Answers

Context menus do not support icons.

Note: Context menu items do not support icons or shortcut keys.

like image 90
CommonsWare Avatar answered Sep 18 '22 13:09

CommonsWare


I did it by this way:

Reference screenshot:

enter image description here

Menu:

menu_patient_language.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".activities.PatientSelectionActivity">

    <item
        android:id="@+id/menuEnglish"
        android:icon="@drawable/language_english"
        android:title="@string/english" />

    <item
        android:id="@+id/menuFrench"
        android:icon="@drawable/language_french"
        android:title="@string/french" />

</menu>

Style:

style.xml:

  <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->

        <item name="android:popupMenuStyle">@style/popupMenuStyle</item>

    </style>

  <!--- Language selection popup -->

    <style name="popupMenuStyle" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:textColor">@color/colorPrimary</item>
        <item name="android:itemBackground">@android:color/white</item>
    </style>

Java code:

 private void showPopup(View v) {

        Context wrapper = new ContextThemeWrapper(this, R.style.popupMenuStyle);
        PopupMenu mypopupmenu = new PopupMenu(wrapper, v);

        setForceShowIcon(mypopupmenu);
        MenuInflater inflater = mypopupmenu.getMenuInflater();
        inflater.inflate(R.menu.menu_patient_language, mypopupmenu.getMenu());
        mypopupmenu.show();
//        mypopupmenu.getMenu().getItem(0).setIcon(getResources().getDrawable(R.mipmap.ic_launcher));
        mypopupmenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                txtPreferredLanguage.setText(item.getTitle().toString());
                switch (item.getItemId()) {
                    case R.id.menuEnglish:
                        // Your code goes here
                        break;

                    case R.id.menuFrench:
                        // Your code goes here
                        break;
                }
                return false;
            }
        });
    }

    private void setForceShowIcon(PopupMenu popupMenu) {
        try {
            Field[] mFields = popupMenu.getClass().getDeclaredFields();
            for (Field field : mFields) {
                if ("mPopup".equals(field.getName())) {
                    field.setAccessible(true);
                    Object menuPopupHelper = field.get(popupMenu);
                    Class<?> popupHelper = Class.forName(menuPopupHelper.getClass().getName());
                    Method mMethods = popupHelper.getMethod("setForceShowIcon", boolean.class);
                    mMethods.invoke(menuPopupHelper, true);
                    break;
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

Hope this would help you sure.

Done

like image 28
Hiren Patel Avatar answered Sep 18 '22 13:09

Hiren Patel