I've searched alot around the internet. I've done what I had to, but still I'm unable to show the 3 dotted menu as I want.
Here's the relevant code:
First, my manifest min sdk is set to 9 and my targetSdk is set to 18.
The menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/itemMainAlbum"
android:showAsAction="ifRoom"
android:title="Set as Main Album">
</item>
<item
android:id="#+id/itemImport"
android:showAsAction="ifRoom"
android:title="Import">
</item>
<item
android:id="#+id/itemSettings"
android:showAsAction="ifRoom"
android:title="Settings">
</item>
The Activity's relevent code (extends SherlockActivity):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.gallery_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.itemImport:
startImportActivity();
return true;
case R.id.itemMainAlbum:
setMainAlbum();
return true;
case R.id.itemSettings:
// do s.th.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here's how my activity looks like:
The menu item in the bottom is shown only when I click on my device's menu button. The 3 dotted menu is just never shown, no matter how many menu items I add.
If you use ActionBarSherlock, you can use this little hack. It works flawlessly for me and shows the menuoverflow on every device I have tested it on.
The "3 dotted menu" will never be shown, as your device has a MENU button. The "3 dotted menu" will only appear on devices that lack a MENU button, to allow such users the ability to access the overflow. You can see what the "3 dotted menu" looks like by setting up an emulator that emulates a device with no MENU button.
You can read more about this in the "Say Goodbye to the MENU Button" Android Developers Blog post.
Related
I can't show items inside the "Menu Item" when those items are already displayed on the Action bar.
This is my onCreateOptionsMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.opt_menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
This is my opt_menu_main layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="ifRoom"/>
</menu>
I'm not using support library (my target API is 17).
If I click on the button Menu it is always empty because the item is already displayed up on the ActionBar.
I've tried to add more items but when the items goes up on the ActionBar, they are not displayed inside the options menu.
I've also tried with showAsAction="ifRoom|withText"but doesn't work.
I think this behavior is correct, but is it possible to show the same item both in Menu and Action Bar at the same time?
Thanks
Try this:
app:showAsAction="always"
but don't forget to add this line at the begin of definition:
xmlns:app="http://schemas.android.com/apk/res-auto"
The Id's for menu items must be unique. The best option is to create another menu item with another Id and set its showAsAction to "never" to force it always into the overflow menu. But their is a chance if you are also displaying other primary options it might force both into the overflow in which case you will see two menu items with the same title.
For the moment I solved like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always"/>
<item
android:id="#+id/menu_settings"
android:title="Settings"
android:showAsAction="never"/>
</menu>
So in the onOptionsItemSelected method :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add_schedule:
case R.id.menu_act_add_schedule:
//some code
break;
}
return true;
}
I don't think this is a correct solution, because on some device where there is not enough space to add this and other items up on the ActionBar,
since they are defined as showAsAction="always", they will be overlapped on the ActionBar Title, so it can create some layout problems.
The behavior that I want is to show always the items on the option menu, and show it also in the action bar if there is enough space.
This behavior can not be obtained with showAsAction="always" .
It would be great if someone could find a better solution.
Thanks anyway.
I am developing an Android app with ActionBarCompat (released by google). In tablets,it shows more menu items as a dropdown menu at top right (Image 1) but in small devices (handsets) you should hit menu button to see more items (Image 2).
Is there any way to have top right dropdown menu like tablets in handset devices? If yes, how?
Thank you.
Sure you can !
First of all, make a menu.xml in res > menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:technicachat="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<item
android:id="#+id/dropdown_menu"
android:showAsAction="ifRoom"
technicachat:showAsAction="ifRoom"
android:title="Options"
android:icon="#drawable/ic_menu_moreoverflow_normal_holo_light">
<menu>
<item
android:id="#+id/item1"
android:showAsAction="ifRoom"
technicachat:showAsAction="ifRoom"
android:title="Refresh"
android:icon="#drawable/ic_menu_refresh"/>
</menu>
</item>
</menu>
All items under the second menu will appear when you click on the dropdown menu button.
You don't even need to use onOptionsItemSelected to drop it.
Just proceed as usual to handle item selection.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.item1:
/*DO STUFF*/
}
return super.onOptionsItemSelected(item);
}
In case you want pngs for your apk, they are under /your sdk path/platforms/android-xx/data/res/ and you got drawable-hdpi, mdpi etc. For example the one you need is ic_menu_moreoverflow_normal_holo_light.
Here you are ! ;)
On a Galaxy S3, I have an issue where a button in the ActionBar is configured as showAsAction="always" and being shown on both the hardware overflow menu and the Action buttons. I would like it to not be shown on the hardware overflow menu and only on the Action buttons. I can disable the menuitem in the onCreateOptionsMenu but it will hide the button on both places.
Something to note: if I force the "3 dots" Action Overflow menu to show, the refresh button gets properly hidden from the hardware overflow menu but still doesn't get hidden from the hardware overflow menu.
Something else to note: if I call menu.size() in either onCreateOptionsMenu or onPrepareOptionsMenu, it doesn't reflect the extra button. For example, I have four buttons and the first button is being shown in both the Action buttons and the overflow menu. menu.size() still returns 4 and doesn't seem to realize that it is showing an extra button.
I can't post a screenshot because this is an app for a client but here is my actionbar.xml file. The refresh button shows in both the overflow and the action bar at the top.
actionbar.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/refreshmenuitem"
android:icon="#drawable/refreshicon"
android:title="Refresh"
android:showAsAction="ifRoom"
android:visible="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<item android:id="#+id/helpbutton"
android:title="Help"
android:showAsAction="collapseActionView"
android:icon="#drawable/ic_action_helpbutton" />
<item android:id="#+id/settingbutton"
android:title="Settings"
android:icon="#drawable/ic_action_settingbutton"
android:showAsAction="collapseActionView" />
<item android:id="#+id/importbutton"
android:title="Import file"
android:icon="#drawable/ic_action_importbutton"
android:showAsAction="collapseActionView" />
</menu>
So I figured it out but it is kind of a hack. So if you have a phone such as the Samsung Galaxy S3 that has a hardware menu button, the onCreateOptionsMenu function actually gets called twice. Once for when the Activity gets loaded to load any menu items that should display in the top right and another time when the user presses the hardware menu button. All I did was create a variable called _firstTimeOnCreateOptionsMenu that is set to false after the first time it is onCreateOptionsMenu method:
public boolean onCreateOptionsMenu(Menu menu)
{
// Populates the actionbar/Menu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbarmenu, menu);
boolean hardware = false;
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
{
hardware = ViewConfiguration.get(context).hasPermanentMenuKey();
}
MenuItem b1 = menu.findItem(R.id.refreshmenuitem);
if(!hardware || _firstTimeOnCreateOptionsMenu)
{
b1.setVisible(true);
_firstTimeOnCreateOptionsMenu = false;
}
else
{
b1.setVisible(false);
_firstTimeOnCreateOptionsMenu = true;
}
}
Hopefully this helps anyone else who is having this issue.
Is your targetSdkVersion set to 14?
http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html
I'd be a bit surprised if this was the issue, since you'd expect unknown XML to just be ignored, but according to the documentation <item> does not support android:layout_width and android:layout_height.
This app was designed before the deprecation of the hardware menu button, so now it automatically shows a menu overflow button at the bottom of our app. We'd rather it wedge itself to the right of our 4 tab bar items when needed.
If that's not doable, we'd at least like to be able to center the lonely menu overflow button like it appears on the first pic here -> http://www.droid-life.com/2012/05/30/dear-developers-can-we-quit-with-the-menu-button-already-and-adopt-an-action-overflow/
We are using ActionBarSherlock.
You must add it mannually with setAddOptionMenu(true);
and then create it in xml for example:
<item
android:id="#+id/video"
android:title="#string/video"
android:icon="#drawable/video"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/email"
android:title="#string/email"
android:icon="#drawable/email"
android:showAsAction="ifRoom|withText"
/>
Then inflate it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
return super.onCreateOptionsMenu(menu);
}
if you want to split the actionBar in the bottom of the screen you can use
android:uiOptions=”splitActionBarWhenNarrow”
in your activity in the manifest
hope it helps
I want to build in my application an ActionBar with a button that makes the role of the old hardware menu button. I'm working on 4.0.3 platform with min sdk=8. The problem is i don't have the menu button in ActionBar and always have to press on emulator menu button to show it. Don't know what to do, there is surely a trick.
Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always"
android:title="#string/menu_settings"/>
<item android:id="#+id/menu_help"
android:title="Help">
</item>
</menu>
And prepare the menu in Java code like always:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
I want a button in Actionbar that shows that menu. Possible?
The problem is i don't have the menu button in ActionBar and always have to press on emulator menu button to show it. Don't know what to do, there is surely a trick.
Not really.
The overflow menu affordance will only appear in the action bar on devices that lack an off-screen MENU button. This will be true for most tablets and many phones that originally shipped with Android 4.0 or higher.
For devices that do have a dedicated MENU button, that is used to bring up the overflow menu.