Android - items in submenu do not appear when clicking on parent menu - android

Please..please help!
I'm new with android development. After i create an app with simple menu. i click on the parent menu, i don't see the items in the submenu appeared. I don't know why is that. Enyone know this, please teach me.
i use nexus Emulator to show result. (nesux S (4.0", 480 X 800:ddpi)
create menu in the Menu folder -> menu-demo like:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/addnew" android:title="add new">
<menu>
<item android:id="#+id/name" android:title="add name"></item>
<item android:id="#+id/age" android:title="Add age"></item>
</menu>
</item>
<item android:id="#+id/update" android:title="update"></item>
<item android:id="#+id/delete" android:title="delete"></item>
</menu>
i update Mainactivity.java in the src.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_demo, menu);
return true;
}

You're missing android:showAsAction="[property".
You can use a few parameters for showAsAction, such as always, never, and ifRoom.
Add these properties to your code, so for example, a menu item that I always want to appear on the actionbar would be like
<item
android:id="#+id/test"
android:showAsAction="always"
android:title="test" >
</item
For submenus, you basically want the topmost selection shown as an action always and the submenu shown never.

Related

Removing the settings menu inflator from Actionbar

I have a action bar set up as this:
When I click on it a settings popup appears like this which when pressed takes me to my apps settings page.
My question is how would I remove the settings popup so that it takes me directly to my settings activity when I press the three dotted button? I tried playing around with code below but it yielded no result.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Goto the menu folder of your project. res>>menu and find the xml file representing the
You have to add the android:showAsAction="always" like below:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu"
android:title="#string/menu_settings_item"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_a" />
</menu>
Use the showAsAction parameter.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu"
android:title="#string/menu_settings_item"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_a" />
</menu>
You can specify that your settings option always appears as a button on the action bar. You don't need the three dots in this case.
For instance, you can specify your action bar menu item like this (notice the "always" value):
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always" />

Menu items not displaying on the top right corner of phone

I am developing an app that doesnt show up menu items in the top right corner. In google nexus it shows up but not in micromax canvas HD.
below is my menu xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mnuRecentLookUp" android:title="#string/mnuRecentLookUp" android:icon="#drawable/clock" />
<item android:id="#+id/mnuBookmarks" android:title="#string/mnuBookmarks" android:icon="#drawable/bookmarks" />
<!-- <item android:id="#+id/mnuFontSize" android:title="#string/mnuFontSize" android:icon="#drawable/fontsize"/>-->
<item android:id="#+id/mnuHelp" android:title="#string/mnuHelp" android:icon="#drawable/help" />
<item android:id="#+id/mnuClose" android:title="#string/mnuClose" android:icon="#drawable/ic_menu_close" />
</menu>
#Override
public boolean onCreateOptionsMenu( final Menu menu )
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.nav_home, menu);
// Calling super after populating the menu is necessary here to ensure that the
// action bar helpers have a chance to handle this event.
return super.onCreateOptionsMenu(menu);
}
I tried overflow to show the 3 dot icon on top right corner.It works fine in 4.0 or more versions not in 2.2-2.3.
in android 2.3, it gives noSuchFieldException {shasPermanentmenukey}.
Please help me to solve the issue. I want the things to work on all android versions i.i. 2.2 and above.
Thanks in advance....
I had the same problem, overflow could not be visible for API < 11.
I tried this,
Add a menu item with icon i.e. image of three dots, and put another menu items inside that item tag. And set app:showAsAction="always" to that item with three dot image
<?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/menu_three_dots"
android:title="#string/menu"
android:icon="#drawable/three_dots"
app:showAsAction="always">
<item android:id="#+id/mnuRecentLookUp"
android:title="#string/mnuRecentLookUp"
android:icon="#drawable/clock" />
<item android:id="#+id/mnuBookmarks"
android:title="#string/mnuBookmarks"
android:icon="#drawable/bookmarks" />
<item android:id="#+id/mnuHelp"
android:title="#string/mnuHelp"
android:icon="#drawable/help" />
<item android:id="#+id/mnuClose"
android:title="#string/mnuClose"
android:icon="#drawable/ic_menu_close" />
</item>
</menu>
Now the menu will be visible at top right corner, just click that three dots and it will show the overflow.

Android Navigation drawer: How to change actionBarMenu according to drawer list item select

I have a Navigation drawer in my app. It has a list of items. I use a switch statement and replace a current Fragment with a completely new Fragment which has its own views and logic.
My problem is that each Fragment has its own menu item in the action bar. I don't know how to change the actionBar menu item according to the selected drawer list item. I have tried lots of tutorials and ended with no result. Some sample code would really help.
Thanks in advance.
I think this tutorial is exactly what you're looking for:
http://www.grokkingandroid.com/adding-action-items-from-within-fragments/
create menuitemlist.xml in your menu folder. You can define your menu and sub menu here. EX:
<item
android:id="#+id/filter"
android:showAsAction="ifRoom"
android:icon="#drawable/filter_data"
android:title="projectlist"
android:titleCondensed="FILTER">
<menu>
<item
android:id="#+id/search"
android:title="Search"/>
</menu>
</item>
<item
android:id="#+id/profilename"
android:showAsAction="ifRoom"
android:title="details"
android:titleCondensed="fullmode">
</item>
<item
android:id="#+id/login"
android:showAsAction="always"
android:title="here"
android:titleCondensed="about">
<menu>
<item
android:id="#+id/it1"
android:title="list1"/>
<item
android:id="#+id/it2"
android:title="list2"/>
<item
android:id="#+id/it3"
android:title="list3"/>
</menu>
</item>
<item
android:id="#+id/admin"
android:icon="#drawable/admin"
android:showAsAction="always"
android:title="welcome"
android:titleCondensed="welcome">
</item>
In activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menuitemlist, menu);
return true;
}
For More Check
http://www.grokkingandroid.com/adding-actionbarsherlock-to-your-project and enjoy with action bar .`

ActionBarSherlock narrow items in ActionBar are not centered

I'm using actionbarsherlock library, activity has flag android:uiOptions="splitActionBarWhenNarrow", so ActionBar's items are in the bottom of screen
the items are described in the xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_0"
android:title="#string/action_0"
android:showAsAction="always"/>
<item android:id="#+id/menu_1"
android:title="#string/action_1"
android:showAsAction="always"/>
<item android:id="#+id/menu_2"
android:title="#string/action_2"
android:showAsAction="always"/>
</menu>
I'm inflating menu
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
MenuInflater supportMenuInflater = getSupportMenuInflater();
supportMenuInflater.inflate(R.menu.inbox_conversation, menu);
return super.onCreateOptionsMenu(menu);
}
after, on devices with android 2.3, menu items do not have the same weight
first two options take about 80% percents of action bar & the latest takes only 20%, so they are not aligned properly & don't take equal space.
Don't know what to do. Any suggestions?
Many thanks!
might want to try adding layout-weight in item tag in xml
<item
android:id="#+id/menu_0"
android:title="#string/action_0"
android:showAsAction="always"
android:layout_width="wrap_content"
android:layout_weight="1" >
</item>

Android - Remove "More actions" button from the ActionBar

I have an ActionBar that should display the action buttons in a custom way. For this I created a custom view and attached it to the ActionBar.
One thing to mention is that I am using a menu.xml resoure file to load the options menu and display them on a smartphone, but do not display them on tablet, instead use a custom view. For this I market every menu item in the xml as: android:showAsAction="never"
Everything looks fine, except one little thing that still remains on the right of the ActionBar - the "More" button.
How can I remove it?
I tried this:
ActionBar bar = activity.getActionBar();
bar.removeAllTabs();
but the "more" button still remains there.
EDIT:
This is my menu.xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_username"
android:icon="#drawable/menu_username"
android:orderInCategory="0"
android:showAsAction="never"
android:title="#string/menu_username">
<menu>
<item
android:id="#+id/menu_logout"
android:title="#string/menu_logout"/>
</menu>
</item>
<item
android:id="#+id/menu_settings"
android:icon="#drawable/menu_settings"
android:orderInCategory="1"
android:showAsAction="never"
android:title="#string/menu_settings"/>
<item
android:id="#+id/menu_search"
android:icon="#drawable/menu_search"
android:orderInCategory="1"
android:showAsAction="never"
android:title="#string/menu_search"/>
</menu>
Please note I still want to inflate this menu on a smartphone, but don't want to use it on a tablet.
Setting showAsAction="never" will force an menu item into the overflow. Why not check in onCreateOptionsMenu(...) that the device is a tablet, and if it is just not inflate the menu? Something like this:
public boolean onCreateOptionsMenu(Menu menu) {
if (getResources().getConfiguration().smallestScreenWidthDp >= 600) {
//It's a tablet, don't inflate, only create the manual view
manualMenuCreation();
} else {
getMenuInflater().inflate(R.menu.menu, menu);
}
return true;
}
Don't forget smallestScreenWidthDp is only available in 3.2 or above, so you'll have to take that into consideration.

Categories

Resources