Adding an icon with submenu items

I made a submenu with some elements in it. Now I want to add another icon with submenu items. I also applied the code to the menu items to add an icon, but it does not show me the icons, which my code is as follows:

public boolean onCreateOptionsMEnu(Menu m) { SubMenu s1 = m.addSubMenu("menu1"); s1.add(0,0,1,"1").setIcon(R.drawable.icon1); s1.add(0,1,2,"2").setIcon(R.drawable.icon2); return super.onCreateOptionsMenu(m); } 
+4
source share
4 answers

Submenus do not support icons.

https://developer.android.com/guide/topics/ui/menus.html :

Icon menu
This is a set of items that initially appear at the bottom of the screen when you press the MENU key. It supports a maximum of six menu items. THESE are the only menu items that support icons and only menu items that do not support checkboxes or radio buttons.

+3
source

Below is a list of standard icons . I do not see the settings icon. Perhaps you mean "Preferences" (ic_menu_preferences)?

You can set the icon programmatically as follows:

 menu.add(0, MENU_QUIT, 0, "Quit").setIcon(R.drawable.menu_quit_icon); 

You can also set it in your XML layout as follows:

 <item android:id="@+id/save_button" android:icon="@android:drawable/ic_menu_save" android:title="Save Image"/> 

- Create a menu in Android

+2
source

Try the following:

 public boolean onCreateOptionsMenu(Menu m) { SubMenu s1 = m.addSubMenu("menu1"); s1.add(0,0,1,"1"); s1.add(0,1,2,"2"); MenuItem menuItem = s1.getItem(0); menuItem.setIcon(R.drawable.icon1); MenuItem menuItem1 = s1.getItem(1); menuItem1.setIcon(R.drawable.icon2); return super.onCreateOptionsMenu(m); } 
0
source

According to the Android documentation , submenus do not support icons.

Submenus do not support item icons or submenus

0
source

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


All Articles