Android ContextMenu setting with icon

Hello everybody

I have a simple question. Can I add a menu item with an icon to the context menu? I searched for this problem, and all I found is that it is not possible, but on the main screen of the Android device, when I do a long click, the β€œadd to home” context menu is displayed, contains menu items with text and an icon, therefore I decided that there should be a way to do this.

I tried to use the MenuItem.setIcon() method, but the icon did not disappear in the context menu, but only the text.

Thanks!

+4
source share
2 answers

Wherever you see icons, this is not a context menu. If it looks a bit like a context menu but has icons, perhaps an AlertDialog with a custom ListAdapter that uses ListAdapter strings.

+9
source

you need to expand your adapter

  public class Menu_adapter extends BaseAdapter { 

and 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; } 
+2
source

Source: https://habr.com/ru/post/1299578/


All Articles