Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ContextMenu option with icon

Tags:

android

Hi All

I have a simple question - It is possible to add a menu item with an icon to a context menu ? I've searched this issue and all I found is that it is not possible, but in the Home screen of the Android device when I perform long-click a "add to home" context menu is being displayed, contains menu items with text and icon, so I figured there got to be a way of doing it.

I tried using the MenuItem.setIcon() method but the icon is not disaplyed in the context menu, only the text.

Thanks!

like image 498
WhiteTigerK Avatar asked Dec 22 '22 06:12

WhiteTigerK


2 Answers

Wherever you see icons, that's not a context menu. If it feels a bit like a context menu but has icons, that's probably an AlertDialog with a custom ListAdapter that uses rows with icons.

like image 94
CommonsWare Avatar answered Dec 30 '22 20:12

CommonsWare


you need to expand your adapter

  public class Menu_adapter extends BaseAdapter {

and the method

public View getView(int position, View convertView, ViewGroup parent) {
            Menu_item menu_item = (Menu_item) this.getItem(position);
            ViewHolder viewHolder;

            if (convertView == null) {
                convertView = lInflater.inflate(R.layout.item_left_elements, null); 
                viewHolder = new ViewHolder();

                viewHolder.text = (TextView) convertView
                        .findViewById(R.id.tvDescr);

                convertView.setTag(viewHolder);
                convertView.setTag(R.id.tvDescr, viewHolder.text);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }



            ImageView imageView = (ImageView) convertView.findViewById(R.id.ivImage);
            if (menu_item.get_Item_Use() == true ) {

                imageView.setImageResource(R.drawable.ic_menu_arrow_icon_pressed);
            } else {    

                imageView.setImageResource(R.drawable.ic_menu_arrow_icon);


            }

            viewHolder.text.setTag(position);
            viewHolder.text.setText(res.getString(menu_item.get_Item_id()));
            return convertView;
        }
like image 26
Max Usanin Avatar answered Dec 30 '22 19:12

Max Usanin