How to display Menu Items overflow android - android

I want to display my menu items on ActionBar with android
I found some samples but It was not what I wanted
Menu should be similar picture below(Overflow Icon or text)
My menu.xml is :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/icon"
android:visible="true"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:title="Settings">
<item
android:id="#+id/mapMenu"
android:title="map menu" />
<item
android:id="#+id/favMenu"
android:title="favorite" />
<item
android:id="#+id/listMenu"
android:title="List Menu" />
</item>
and my android code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getApplicationContext(), item.getTitle() + " selected", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.mapMenu:
// do something
break;
case R.id.favMenu:
// do something
break;
case R.id.listMenu:
// do something
break;
}
return true;
}

In some Android versions or on some devices, I think, the overflow icon would not be displayed, and you would have to press the menu button on the phone.
With what you have at the moment, the overflow icon would be displayed on some devices and not on some others.
Using the support library
If you really want to have an overflow icon, you could use the appcompat support library for Android 5.0.
To do that, add this into your build.gradle file, under dependencies:
compile 'com.android.support:appcompat-v7:21.0.3'
Also, you need to extend ActionBarActivity in your activity instead of Activity.
And in styles.xml, make sure your parent theme has the prefix Theme.AppCompat.
You can read more about that here.

Try android:showAsAction="never" for item which you want to show in overflow menu . I think this is what you want.

Related

Menu item goes in dropdown list menu not in the action bar

Hello
I'm trying to add 2 menu items in the action bar. On the designer they look ok, but when I run the application, both menu items goes in the dropdown list hamburger menu (there's enough "room" to display on the action bar).
I tried to replace app:showAsAction to android:showAsAction, doesn't work this replacement.
this is my menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.tabdemo.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title=""
app:showAsAction="never"
/>
<item
android:id="#+id/userMenu"
android:title="User"
app:showAsAction="ifRoom" />
<item
android:id="#+id/logoutMenu"
android:icon="#drawable/opendoorlogo2"
android:title="Logout"
app:showAsAction="ifRoom" />
And this is the java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem userMenuItem = menu.findItem(R.id.userMenu);
userMenuItem.setTitle(username);// global string
MenuItem logoutMenuItem = menu.findItem(R.id.logoutMenu);
logoutMenuItem.setIcon(R.drawable.opendoorlogo2);
return true;
}
Thanks in advance (P.S. Logo doesn't load on Logout menu Item)
First of all make sure your drawable file is not too big for the actionbar, if so you can convert it to actionbar icon size.
https://romannurik.github.io/AndroidAssetStudio/icons-actionbar.html#source.space.trim=1&source.space.pad=0&name=ic_action_example&theme=light&color=33b5e5%2C60
android:showAsAction="always"
if this doesn't help you.
Best way is to create a custom actionbar layout and place your icon over there instead of adding it as Menus.

Action bar buttons only showing in the overflow

I tried to add buttons with icons to the Action Bar so I followed the info on the android developer page. However despite having assigned icons, the buttons will only be displayed in the overflow and not on the bar itself. I want the buttons to appear on the bar with their icons. My code:
The onOptionsItemSelected method of the activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_new:
launchAddRuleActivity();
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The onCreateOptionsMenu of the activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
The main.xml for the action bar layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.mhmt.autotextmate.Main" >
<!-- New, should appear as action button -->
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
android:showAsAction="ifRoom"
android:title="#string/action_new"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_save"
android:showAsAction="never"
android:title="#string/action_settings"/>
Lastly, the target and min SDKs:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="22" />
I got the drawables from here so the size shouldnt be a problem. This class does extend ActionBarActivity, and imports android.support.v7.app.ActionBarActivity. I'm currently testing on a Google Nexus emulator running Android 5.1.1 with 720x1280 resolution.
I must be doing something wrong, can anyone see what I'm missing to display the buttons with their icons, and not in the overflow?
Try this app:showAsAction="always"
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.mhmt.autotextmate.Main" >
<!-- New, should appear as action button -->
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
app:showAsAction="always"
android:title="#string/action_new"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_save"
app:showAsAction="always"
android:title="#string/action_settings"/>

The menu item isn't showing in the ActionBar, only in overflow menu

I have exactly the same problem as in these threads:
No actions or icons showing on the actionbar; only in overflow
Menu items won't show up in the actiobar
No one has offered a solution, so I want to bring it up again.
I have a xml-file, my_menu.xml, that look like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/delete"
android:showAsAction="ifRoom|withText"
android:icon="#drawable/ic_delete_image"
android:title="delete"/>
</menu>
My activity extends ActionBarActivity and implements these methods:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.delete:
//delete something
break;
}
return(true);
}
In my book: "A value of ifRoom means that the menu item will appear in the action bar if there is space for it" - I have tested to use ifRoom, ifRoom|withText and always, but the item always ends up in the overflow menu. I have tested the application on different devices, and it's plenty of room. Do anyone have a solution to this?
Hank
My activity extends ActionBarActivity
As is covered in the documentation, if you are using the appcompat_v7 backport of the action bar, and its associated ActionBarActivity, your menu resource needs to have showAsAction in a namespace custom to your app:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/delete"
yourapp:showAsAction="ifRoom|withText"
android:icon="#drawable/ic_delete_image"
android:title="delete"/>
</menu>
Here is a complete sample project demonstrating the use of the appcompat_v7 action bar.
Try changing it to
<item
android:id="#+id/delete"
android:showAsAction="always|withText"
android:icon="#drawable/ic_delete_image"
android:title="delete"/>
Hope this will help you.

how to put icons on menu_item in android apps?

Why the icons didn't appear when i used the menu.xml below !!. Please help me.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="#+id/group1">
<item android:id="#+id/item1" android:title="Embelance" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher" >
<menu>
<item android:id="#+id/E_H1" android:title="To home" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher"/>
<item android:id="#+id/E_W1" android:title="To work" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher"/>
<item android:id="#+id/E_O1" android:title="To oppional" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher"/>
</menu>
</item>
<item android:id="#+id/item2" android:title="Fier" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher">
<menu>
<item android:id="#+id/F_H1" android:title="To Home" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher"/>
<item android:id="#+id/F_W1" android:title="To work" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher"/>
<item android:id="#+id/F_O1" android:title="To Optional" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher"/>
</menu>
</item>
</group>
and when used the next attribute it shows that there is error in the app. : the window
says that the project contains error(s),fix it before the its running or something like that.
android:icon="#android:drawable/ic_launcher"
and the code is:
#Override
public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.main_context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
// .......
switch(item.getItemId()){
case R.id.E_H1: // ...........
break;
case...case....case...... // other cases
return super.onContextItemSelected(item);
}
I'll try to answer this, but so far the question is not 100% clear to me. Based on the comments, it seems you wish to know how to combine icons with text in menu item.
For that you have provided the answer yourself in your question: the 3-rd item contains what you need. If you wish to group items in a menu item block, then you can use groups as you did, but specifically for your example, the group is point-less. Something like below will suffice (running on a Motorola Defy 2.3.6 and on Nexus 4):
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/context_setting_item1"
android:icon="#drawable/ic_launcher"
android:title="#string/action_settings">
</item>
</menu>
In your Activity you will have (suppose menu is in /res/menu/entry.xml):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.entry, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.context_setting_item1:
Toast.makeText(this, "context_setting_item1 selected", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Now, to go back to the menu item general discussion: these visual components have been replaced by ActionBar - it is more friendly, more straight-forward when it comes to expose the content/app-hierarchy and it is sexy! :)
To dive into development, you can check this Android developers blogs, "Goodbye to MenuItem". For more development details check the developer section. From programming perspective it's not that much of big deal as the action bar components are developed with menu items in xml. You just need to provide more XML attributes.
Ex.: Only the above xml changes to:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/context_setting_item1"
android:icon="#drawable/ic_launcher"
android:showAsAction="always|withText"
android:title="#string/action_settings">
</item>
</menu>
And the Activity code remains as it is.
Note that I have added only android:showAsAction="always|withText".
The fun part is that nothing changes on Motorola 2.3.6 (since ActionBar was added in API level 11), but on Nexus you'll notice the top action bar has the icon on top. Previously, it had a 3-dots and the user had to tap on them in order to view the action.
If you wish to have the action bar in api-level below 11, I strongly recommend using Sherlock ActionBar.

Android action bar not showing overflow

I have an action bar in my app with 3 items.
Only 2 can be displayed due to space issues, so I'd expect the first to be displayed and the rest to be displayed in the overflow.
However in practice only the first 2 items are shown and there is no overflow detectable.
Here is the relevant code:
list_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_insert"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/menu_insert"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_call"
android:icon="#android:drawable/ic_menu_call"
android:title="#string/menu_call"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_agenda"
android:icon="#android:drawable/ic_menu_agenda"
android:title="#string/menu_agenda"
android:showAsAction="ifRoom|withText"/>
</menu>
Activity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu, menu);
return true;
}
If you want to show the three dots, irrespective of device menu button! then you can call this method in your application class' onCreate method-
private void makeActionOverflowMenuShown() {
//devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
}
Output
res/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search / will display always -->
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:title="#string/action_search"/>
<!-- Location Found -->
<item
android:id="#+id/action_location_found"
android:icon="#drawable/ic_action_location_found"
android:showAsAction="always"
android:title="#string/action_location_found"/>
<!-- More -->
<item
android:id="#+id/a_More"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="always"
android:title="More">
<menu>
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_refresh"/>
<!-- Help -->
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:showAsAction="never"
android:title="#string/action_help"/>
<!-- Check updates -->
<item
android:id="#+id/action_check_updates"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_check_updates"/>
</menu>
</item>
</menu>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
Download the Action Bar Icon Set
I realize this is not an overflow menu, but it is similar.
Okay, this is simple but was hard to figure out.
You first need a menu item you want to use as the overflow inflater. Example
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
</item>
Once you have your item, add a sub-menu containing your items you want in the overflow menu. Example:
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="#+id/aM_Home"
android:icon="#drawable/home"
android:title="Home"/>
</menu>
</item>
On click this will inflate other items within. My application is using ActionBarSherlock 4.0 so before this will work for you, you will need to access the "SplitActionBar". (Will still work on default android Actionbar)
Here's how:
In your AndroidManifest.xml file, you need to add this code under the activity you need the overflow menu in.
android:uiOptions="splitActionBarWhenNarrow"
NOTE: Your item that inflates your overflow menu MUST showAsAction="always"
Vwola! you have an overflow menu! Hope I helped you out. :)
On devices with hardware menu buttons (Galaxy S3, stubborn as Samsung is...) the overflow menu behaves as the 'traditional' menu, by using the hardware menu button.
When you say "overflow" menu, do you mean the three dots that show up in the end to indicate that there are more items.... or do you mean the split actionbar that shows up in the bottom for overflow items?
If you mean the split action bar, you should add this to your activity's manifest file
android:uiOptions="splitActionBarWhenNarrow"
By default, the three dots overflow menu should happen automatically, so it's hard to tell what the problem is from the information you provided above.
To alway show action overflow (three dot) on actionbarcompat:
in menu file, example:
main.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_settings"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_about"
android:title="#string/action_about"
app:showAsAction="never"/>
</menu>
and in activity file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
It's worked fine for me.
Tested on Google Nexus S, Samsung S3.
It appears that on devices with a menu button the overflow menu does not show in the ActionBar. Also we have no way to know if a device has a hardware menu button before API level 14 (4.0+).
This solution adds a overflow button to a android.support.v7.app.ActionBar that opens the standard options menu on pre-Honeycomb (<3.0) only, otherwise leaving it to the system. In any case you can choose between always or ifRoom for all actions.
Get the overflow icon from the Action Bar Icon Pack.
Add a menu (I'omitting some non-relevant parts as titles, ordering etc.)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_overflow"
android:orderInCategory="100"
android:icon="#drawable/ic_action_overflow"
app:showAsAction="always" />
<item
android:id="#+id/action_first"
app:showAsAction="always" />
<item
android:id="#+id/action_second"
app:showAsAction="ifRoom" />
</menu>
Remove our overflow action on 3.0+
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
menu.removeItem(R.id.action_overflow);
}
return super.onPrepareOptionsMenu(menu);
}
Finally call Activity.openOptionsMenu() from the overflow action.
Er... no. You cannot not call openOptionsMenu() from a menu button... but if you are using AndroidAnnotations you're done easily:
#OptionsItem
void action_overflow() {
openOptionsMenuDeferred();
}
#UiThread
void openOptionsMenuDeferred() {
openOptionsMenu();
}
If not you should do something like this, I suppose
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == id.action_overflow) {
openOptionsMenuDeferred();
return true;
}
}
private Handler handler = new Handler(Looper.getMainLooper());
public void openOptionsMenuDeferred() {
handler.post(new Runnable() {
#Override
public void run() {
openOptionsMenu();
}
}
);
}
Try changing the theme of the app from
Theme.AppCompat.Light.DarkActionBar to Theme.AppCompat.Light
Put xmlns:your_app_name="http://schemas.android.com/apk/res-auto" inside menu tag
and instead android:showAsAction="always" use your_app_name:showAsAction="always"
for eg
<item
android:id="#+id/action_search"
android:icon="#drawable/icon_search"
android:title="#string/search"
your_app_name:showAsAction="ifRoom"/>

Categories

Resources