In some pages of stackoverflow said that you can get menu goup with menu.findItem(R.id.groupId) but always return null and i want to know how can i get menu group
<?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">
<group android:id="#+id/fdp_m_group_normal">
<item android:id="#+id/fdp_m_today"
app:actionLayout="#layout/menu_today"
app:showAsAction="always|withText"
android:orderInCategory="100"
android:title="today"/>
<item android:id="#+id/fdp_m_tomorrow"
app:actionLayout="#layout/menu_tomorrow"
app:showAsAction="always|withText"
android:orderInCategory="101"
android:title="tomorrow"/>
<item android:id="#+id/fdp_m_after"
app:actionLayout="#layout/menu_after"
app:showAsAction="always|withText"
android:orderInCategory="102"
android:title="after"/>
</group>
<group android:id="#+id/fdp_m_group_range">
<item android:id="#+id/fdp_m_plus1"
app:actionLayout="#layout/menu_plus1"
app:showAsAction="always|withText"
android:orderInCategory="103"
android:title="+1"/>
<item android:id="#+id/fdp_m_plus2"
app:actionLayout="#layout/menu_plus2"
app:showAsAction="always|withText"
android:orderInCategory="104"
android:title="+2"/>
<item android:id="#+id/fdp_m_plus3"
app:actionLayout="#layout/menu_plus3"
app:showAsAction="always|withText"
android:orderInCategory="105"
android:title="+3"/>
</group>
</menu>
and my java code for accessing menu
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fdp_menu, menu);
// Below line always return null
MenuItem mGroupOne = menu.findItem(R.id.fdp_m_group_normal);
super.onCreateOptionsMenu(menu, inflater);
}
Getting group using findItem does return null. I am assuming you only need it for visibility. Try using menu.setGroupVisible(R.id,false);
menu.findItem will return null if there is no item in 'menu' with the id.
So first of all check that 'menu' is the right menu to be looking in, and then check in menu.xml that you have declared an item with the correct id.
Related
I'm able to create an option menu in Android through XML file, but I would like to add the second menu programmatically just before the menu is added from XML.
In example: the code below adds a menu item search.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
If I add a new menu item programatically, like
menu.add(0,Menu.FIRST, Menu.NONE,"Gift box").setIcon(R.drawable.jingle_bell).
setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
It is appearing after the Search menu item in Actionbar, but I need it before the search menu item.
What changes do I need to make?
Try this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.yourmenu, menu);
menu.add(0,Menu.FIRST, Menu.NONE,"Gift box").setIcon(R.drawable.ic_refresh).
setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Update:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
android:orderInCategory="200"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
Just set order for your item as,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.yourmenu, menu);
menu.add(Menu.NONE, Menu.FIRST, 1,"Gift box").setIcon(R.drawable.ic_refresh).
setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Third parameter is order for above menu item. Now in your menu.xml set order for your item as,
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
android:orderInCategory="2"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
Try below example
in XML :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_guide"
android:title="title"
android:icon="#drawable/ic_title"
app:showAsAction="always"
/>
then copy below code to your activity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return true;
}
I am very new to Java Programming and Android Studio.
While creating an Options Menu, I created a list of options in the main menu with two items in a group sub menu.
This is my menu.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"
>
<item
android:id="#+id/giveup_setting"
android:orderInCategory="1"
app:showAsAction="ifRoom"
android:title="#string/action_option1"/>
<item
android:id="#+id/new_game_setting"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/action_option2"/>
<item
android:id="#+id/help_setting"
android:orderInCategory="3"
app:showAsAction="never"
android:title="#string/action_option3"/>
<item
android:id="#+id/settings_setting"
android:orderInCategory="4"
app:showAsAction="never"
android:title="#string/action_option4" />
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/menu_color"
android:orderInCategory="1"
app:showAsAction="never"
android:title="#string/layout_color"/>
<item
android:id="#+id/menu_text"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/layout_text"/>
</group>
</menu>
<item
android:id="#+id/about_setting"
android:orderInCategory="5"
app:showAsAction="never"
android:title="#string/action_option5"/>
<item
android:id="#+id/exit_setting"
android:orderInCategory="6"
app:showAsAction="never"
android:title="#string/action_option6"/>
</menu>
An this is how I inflate the menu in my MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.clear();
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
// Handle item selection
switch (menu.getItemId()) {
case R.id.help_setting:
//show help screens
return true;
case R.id.menu_color:
//change a layout of my main activity
return true;
case R.id.menu_text:
//change another layout of my main activity
return true;*/
case R.id.about_setting:
//show my about screen
return true;
case R.id.giveup_setting:
//resets the game
case R.id.new_game_setting:
//restarts the app
return true;
case R.id.exit_setting:
//exits the app
return true;
default:
return super.onOptionsItemSelected(menu);
}
}
On running the app, the menu populates but the "Settings" item (which has the nested group radio buttons) displays twice.
On running the app, the menu populates but the "Settings" item (which has the nested group radio buttons) displays twice.
What am I doing wrong?
if android:id="#+id/settings_setting" is the one you want to have a submenu don't close the item tag. Instead inflate <menu> inside the <item> tag.
<item
android:id="#+id/settings_setting"
android:orderInCategory="4"
app:showAsAction="never"
android:title="#string/action_option4">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/menu_color"
android:orderInCategory="1"
app:showAsAction="never"
android:title="#string/layout_color"/>
<item
android:id="#+id/menu_text"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/layout_text"/>
</group>
</menu>
</item>
I want the menu items to show on action bar. There is a lot of space on the ActionBar, but still they don't show up. Every item shown as three dots, why?
.java file
public class GalleryActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_layout);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
}
.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MissionAndroid="http://schemas.android.com/tools">
<item
android:id="#+id/facebookId"
android:title="facebook"
android:icon="#drawable/facebook"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/shareId"
android:title="share"
android:icon="#drawable/share"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/delete"
android:title="delete"
android:icon="#drawable/delete"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/searchId"
android:title="search"
android:icon="#drawable/search"
MissionAndroid:showAsAction="ifRoom"/>
</menu>
Currently, you're using "http://schemas.android.com/tools" which does not offer the functionality you're looking for and your showAsAction modifiers are being ignored.
Try updating your main_menu.xml with the following:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MissionAndroid="http://schemas.android.com/apk/res-auto">
...
</menu>
Better yet, to follow convention, replace MissionAndroid in this file with app:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/facebookId"
android:title="facebook"
android:icon="#drawable/facebook"
app:showAsAction="always"/>
...
</menu>
I am trying to findview of an overflow icon. After clicking and opening the overflow icon, I tried using in onoptionsitemselected:
View view = getActivity().findViewById(R.id.menu_tag); // null
View view = getActivity().findViewById(R.id.mainMenu); // not null.
<?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">
<item
android:id="#+id/mainMenu"
android:title="#string/text"
android:orderInCategory="101"
android:icon="#drawable/ic_more_vert_white_24dp"
app:showAsAction="always">
<menu>
<item
android:id="#+id/menu_tag"
android:icon="#drawable/tag_32"
app:showAsAction="always|withText"
android:title="#string/tags"/>
<item
android:id="#+id/menu_profile"
android:icon="#drawable/user_32"
app:showAsAction="always|withText"
android:title="#string/profile"/>
<item
android:id="#+id/menu_debug"
android:icon="#drawable/insect_32"
app:showAsAction="always|withText"
android:title="#string/debug"/>
</menu>
</item>
</menu>
It is giving me null but working fine for actionbar items.
You should try to find view in onCreateOptionsMenu method like this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.optionsMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menufile, menu);
MenuItem item = menu.findItem(R.id.mainmenu);
return true;
}
I found a solution by setting actionview to imagebutton and then finding the view.
I want to add a submenu inside my OptionsMenu to a menuItem, programatically according to my parameters. I've checked "MenuItem" in android sdk and there is no addSubMenu() method!, although you can find "hasSubMenu()" and "getSubMenu".
Was thinking on doing this in onCreateOptionsMenu:
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem mi = menu.getItem(MYITEMID); // << this is defined in my XML optionsMenu
SubMenu subm = mi.addSubMenu(0,1,0,"Map 1"); // no addSubMenu() method!!!???
....
How do I create a submenu inside a menuitem in code?
Sometimes Android weirdness is really amazing (and amusing..). I solved it this way:
a) Define in XML a submenu placeholder like this:
<item android:visible="true" android:id="#+id/m_area"
android:titleCondensed="Areas"
android:title="Areas"
android:icon="#drawable/restaur"
android:enabled="true">
<menu>
<item android:id="#+id/item1" android:title="Placeholder"></item>
</menu>
</item>
b) Get sub menu item in OnCreateOptionsMenu, clear it and add my submenu items, like this:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mapoptions, menu);
int idx=0;
SubMenu subm = menu.getItem(MYITEM_INDEX).getSubMenu(); // get my MenuItem with placeholder submenu
subm.clear(); // delete place holder
while(true)
{
anarea = m_areas.GetArea(idx); // get a new area, return null if no more areas
if(anarea == null)
break;
subm.add(0, SUBAREASID+idx, idx, anarea.GetName()); // id is idx+ my constant
++idx;
}
}
I know this is an old question, but I just came across this problem myself.
The most straightforward way of doing this, seems to be to simply specify the item itself as a submenu, then add to this item.
E.g.:
menu.add(groupId, MENU_VIEW, Menu.NONE, getText(R.string.menu_view));
menu.add(groupId, MENU_EDIT, Menu.NONE, getText(R.string.menu_edit));
SubMenu sub=menu.addSubMenu(groupId, MENU_SORT, Menu.NONE, getText(R.string.menu_sort));
sub.add(groupId, MENU_SORT_BY_NAME, Menu.NONE, getText(R.string.menu_sort_by_name));
sub.add(groupId, MENU_SORT_BY_ADDRESS, Menu.NONE, getText(R.string.menu_sort_by_address));
:
:
Here's a complete answer which builds on the idea of using a placeholder but uses mostly xml to add the submenu.
If you have a menu like so called main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="My Menu"
android:id="#+id/my_menu_item">
<!-- A empty SubMenu -->
<menu></menu>
</item>
</menu>
Create another menu sub_menu.xml which will be used in my_menu_item:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="SubMenu One"
android:id="#+id/submenu_one" />
<item android:title="SubMenu Two"
android:id="#+id/submenu_two" />
<item android:title="SubMenu Three"
android:id="#+id/submenu_three" />
</menu>
In your onCreateOptionsMenu:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate your main_menu into the menu
getMenuInflater().inflate(R.menu.main_menu, menu);
// Find the menuItem to add your SubMenu
MenuItem myMenuItem = menu.findItem(R.id.my_menu_item);
// Inflating the sub_menu menu this way, will add its menu items
// to the empty SubMenu you created in the xml
getMenuInflater().inflate(R.menu.sub_menu, myMenuItem.getSubMenu());
}
This solution is nice since the inflater handles most of the work.
The best way to do this is in your xml menu file. You can do this by creating a new menu object inside of an item:
<menu>
<item>
...
<menu>
...
</menu>
...
</item>
</menu>
To provide a comprehensive example of Phil's answer, here is my complete, working XML for a menu with two choices, each of which is a menu with three choices. I intend to add a third menu to the top level ...
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:HTMLCode="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/Examine"
android:title="#string/Examine"
HTMLCode:showAsAction="always">
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:HTMLCode="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/load"
android:title="#string/load"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="#+id/findfirst"
android:title="#string/findfirst"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="#+id/findnext"
android:title="#string/FindNext"
HTMLCode:showAsAction="ifRoom|withText" />
</menu>
</item>
<item android:id="#+id/Redirect"
android:title="#string/Redirect"
HTMLCode:showAsAction="ifRoom|withText">
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:HTMLCode="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/getRedirect"
android:title="#string/getRedirect"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="#+id/toggleRedirect"
android:title="#string/toggleRedirect"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="#+id/copyRedirect"
android:title="#string/copyRedirect"
HTMLCode:showAsAction="ifRoom|withText" />
</menu>
</item>
</menu>
You should consider use a ActionProvider instead.
public class MyActionProvider extends ActionProvider {
private Context mContext;
public MyActionProvider(Context context) {
super(context);
mContext = context;
}
#Override
public View onCreateActionView() {
//LayoutInflater layoutInflater = LayoutInflater.from(mContext);
return null;
}
#Override
public void onPrepareSubMenu(SubMenu subMenu) {
super.onPrepareSubMenu(subMenu);
subMenu.clear();
subMenu.add("menu 1");
subMenu.add("menu 2");
subMenu.add("menu 3");
}
#Override
public boolean hasSubMenu() {
return true;
}
#Override
public boolean onPerformDefaultAction() {
return super.onPerformDefaultAction();
}
}
I would just create the submenu in xml file, and in run time get the submenu from menu object, (using findItem(id) method) and use submenu.setVisible(boolean) to add/remove it on run time.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu1" android:alphabeticShortcut="a"
android:title="Menu No. 1" android:orderInCategory="1" />
<item android:id="#+id/menu2" android:alphabeticShortcut="b"
android:title="Menu No. 2" android:orderInCategory="2">
<menu >
<group android:id="#+id/group2" android:checkableBehavior="single">
<item android:id="#+id/submenu1" android:title="SubMenu No. 1" />
<item android:id="#+id/submenu2" android:title="SubMenu No. 2" />
</group>
</menu>
</item>