How do I show Overflow Menu Items to Action Bar in Android - android

I am writing an Android App in which i am trying to show Overflow Menu Items to ActionBar
using this great tutorial link: http://wptrafficanalyzer.in/blog/adding-action-items-and-overflow-menu-items-to-action-bar-in-android/
Problem:
Not getting Overflow Menu Items (Icon)
Please see below Screen Shot for more clarity:
Manifest.xml:
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:uiOptions="splitActionBarWhenNarrow"
>
items.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/phone"
android:title="#string/phone"
android:icon="#drawable/phone"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/computer"
android:title="#string/computer"
android:icon="#drawable/computer"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/gamepad"
android:title="#string/gamepad"
android:icon="#drawable/gamepad"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/camera"
android:title="#string/camera"
android:icon="#drawable/camera"
android:showAsAction="ifRoom|withText"
/>
<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"
/>
</menu>
I am using this tutorial, and trying to make Figure 6 : Action items and Overflow menu in Split Action Bar
Please help me to show Overflow Menu Items (ICON) to ActionBar
Now whenever i do click on Menu Button in emulator, then i am getting rest Menu Items....

Just to say that If your device has a menu-button, the overflow-icon won't show, on the newer phones the overflow button will show. I would not reccomend ASMUIRTI's answer since it is an awful hack that breaks consistency with the rest of the apps on the platform.
you must use
android:showAsAction="never"
and let the android device decide if the overflow menu is needed for that device.

To show three dot icon, to Action Bar, just use below method in your OnCreate():
private void getOverflowMenu() {
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) {
e.printStackTrace();
}
}

This could be another work around, which really helped me. Keep one drawable with three dots and give it as a menu item.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/saveDetails"
android:showAsAction="always"
android:title="#string/save"/>
<item
android:id="#+id/menu_overflow"
android:icon="#drawable/dts"
android:orderInCategory="11111"
android:showAsAction="always">
<menu>
<item
android:id="#+id/contacts"
android:showAsAction="never"
android:title="Contacts"/>
<item
android:id="#+id/service_Tasks"
android:showAsAction="never"
android:title="Service Tasks"/>
<item
android:id="#+id/charge_summary"
android:showAsAction="never"
android:title="Charge Summary"/>
<item
android:id="#+id/capture_signature"
android:showAsAction="never"
android:title="Capture Signature"/>
</menu>
</item>
</menu>

If I understand your question correctly and you want to show all your icons on the action bar change this parameters in your menu items
android:showAsAction="ifRoom|withText"
to this
android:showAsAction="always"

If you are using from supporting libraries, in order to show menu items in action bar you must use from the following syntax in your xml:
yourappname:showAsAction="ifRoom|withText"

The phones which have menu hardware button show extra menu items on click of the hardware button. Newer phones, with no hardware menu button automatically add an Overflow menu icon to the Action Bar.
The extra menu items are those which have "showAsAction" property set to never.

Within AndroidManifest.xml between add
android:theme="#android:style/Theme.Holo.Light"
which adds the action bar in your application.
After that, go to menu.xml and add the followings
xmlns:tools="http://schemas.android.com/tools
between
Finally in each item add
android:showAsAction="always"
tools:ignore="AppCompatResource"

Related

Hide, unhide ActionBar icons with Search Widget

A common pattern I see in many apps that provide some search functionality is to have the search widget clear the other menu icons off of the ActionBar. This works for me right now just by using ifRoom for showAsAction in my menu items. I'd like to show more menu items in the actionbar though, because it seems that the ActionBar is very conservative with how it determines ifRoom and consequently at least one important menu item is pushed to the overflow (perhaps someone smarter than me knows why they do that).
So how can I set showAsAction as always while still clearing the menu items off of the ActionBar when the Search Widget is activated? When I try that now, the menu items don't move, and the search widget is condensed.
Here's my menu:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="collapseActionView|always"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/action_stores"
android:title="#string/action_stores"
android:icon="#drawable/ic_action_stores"
android:showAsAction="always" />
<item
android:id="#+id/action_shopping_lists"
android:title="#string/action_shopping_lists"
android:icon="#drawable/ic_action_lists"
android:showAsAction="always" />
<item
android:id="#+id/action_preferences"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
My searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
Couldn't you set an onClickListener on the search widget that then runs some few lines of code to make the other MenuItems change from 'always' to 'ifRoom'?
The few lines of code within the onClickListener would contain something like this:
myMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
whereas myMenuItem is your MenuItem that should go from 'always' to 'ifRoom'.
You can change it back later in an appropriate method.

Items in Action Bar won't show in App

I have a problem: I want to put a search Icon on the Action Bar. In Android Studio it shows it on the Action Bar, but when I open the App it only shows it in the menu..
Here is my xml code:
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom"
android:title="#string/action_search"/>
I tried many things but it wont work. I hope you can help me.
It still doesn't work I really don't have a clue why.
You're not specifying your ActionView class, which is probably part of the problem. Here is the xml for a search action view that I use in one of my applications that works fine.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search_user"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/magnifing_glass"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/action_search"/>
</menu>
EDIT:
If you're not trying to use it as an ActionView, disregard the above and use showAsAction="always".
Your problem is either that there is not enough space on the action bar to display the icon, or your device has a dedicated menu button.

Adding Action Bar to an android app

I'm using this guide http://developer.android.com/guide/topics/ui/actionbar.html to create and add items to the action bar in my app. However the items aren't showing up. This is the menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_alert"
android:icon="#drawable/ic_dialog_alert"
android:title="#string/action_alert"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
I already added the theme in the manifest
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo" >
And finally this is the onCreateOptionsMenu in the Activity
#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.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
I know the Action Bar is working fine because I can hide it with this without problems
ActionBar actionBar = getActionBar();
actionBar.hide();
What am I missing?
When working with devices a hardware settings button your overflow button is moved to the settings button, there are two alternatives to get around this:
xml:
Instantiate your own overflow:
<item android:title=""
android:id="#+id/more"
android:showAsAction="always"
android:icon="#drawable/overflow">
<menu>
<item android:id="#+id/action_alert"
android:icon="#drawable/ic_dialog_alert"
android:title="#string/action_alert"
android:showAsAction="never" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
</item>
.java:
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) {
e.printStackTrace();
}
If your concern is that there are no icons showing up, try changind android:showAsAction="..." to android:showAsAction="always". This will explicitly show the icons. This is not a good solution if you're supporting multiple screen resolutions though.
XML Namespace
Try a custom namespace like below in your menu.xml file. You can replace custom by whatever you like.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto" >
And then use that namespace in your items (same xml file) instead of the android namespace
<item android:id="#+id/action_alert"
android:icon="#drawable/ic_dialog_alert"
android:title="#string/action_alert"
custom:showAsAction="ifRoom" />
Complete (working) Example
A more complete example can be found at this menu-drawer-compat-example on github. Content of the menu xml file there is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hcpl="http://schemas.android.com/apk/res-auto" >
<!-- used custom ns hcpl to force display of action buttons -->
<item
android:id="#+id/action_websearch"
android:icon="#drawable/action_search"
android:showAsAction="ifRoom|withText"
android:title="#string/action_websearch"
hcpl:showAsAction="ifRoom"/>
</menu>
This is an example project for the Android compat Actionbar, keep that in mind if you compare your code.

Android menu item not showing drawable

Im using the ActionBarActivity on my activity and inflating the following menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:microecs="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/description_refresh"
android:orderInCategory="1"
microecs:showAsAction="ifRoom" />
<!--TODO: get a good logout icon-->
<item
android:id="#+id/menu_logoff"
android:icon="#drawable/ic_action_logout"
android:title="#string/description_logoff"
android:orderInCategory="2"
microecs:showAsAction="never" />
</menu>
The second menu item drawable that i want to appear as a menu and not in the action bar doesn't not appear in my S4 (running 4.3) and also on lower devices running 2.3.7
Just update your code with following code.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:microecs="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/description_refresh"
android:orderInCategory="1"
microecs:showAsAction="ifRoom" />
<!--TODO: get a good logout icon-->
<item
android:id="#+id/menu_logoff"
android:icon="#drawable/ic_action_logout"
android:title="#string/description_logoff"
android:orderInCategory="2"
microecs:showAsAction="ifRoom" />
</menu>
Whenever you are using never then it dont place that item in the Action Bar.
For about menu refer this link.
This is not possible by design. Overflow menus from the action bar don't show icons - that's just a design decision Google made.
You can check more, e.g. here: Displaying icon for menu items of Action Bar in Honeycomb android 3.0

Android 3.0: Changing order of icons in the action bar

I am working with the Android 3.0.
I have in the action bar 3 items and one of them is the menu of the application, it also has sub-menu in it.
I want to change the order of the icons so the menu won't be in the right place, I want it to be between the two other items. I tried to change the order in the xml with no success.
Does anyone has an idea how to set the order?
Try setting android:menuCategory and android:orderInCategory for your menu items to set the ordering.
Example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/launch_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/search_programs"
android:menuCategory="container"
android:orderInCategory="2"
android:showAsAction="always" />
<item android:id="#+id/preferences_screen"
android:icon="#android:drawable/ic_menu_preferences"
android:title="#string/preferences" />
<item android:id="#+id/refresh"
android:icon="#drawable/ic_menu_refresh"
android:menuCategory="container"
android:orderInCategory="1"
android:title="#string/refresh_items"
android:showAsAction="ifRoom" />
</menu>
It will have the same effect as declaring launch_search -action as last, but this way you'll have your actions in the right order for 2.x and older devices while being able to control action ordering for 3.x -devices.
The menu will always go to the right, you cannot control its placement.

Categories

Resources