I implemented ActionBarCompat to have an ActionBar across different OS versions.
It is hidden on smartphones, and displayed only on tablets.
I see that in this screenshot: http://developer.android.com/resources/samples/ActionBarCompat/index.html, the Share option menu displays the Logout option in a submenu, but when I compiled and run the sample code on a tablet, the Logout option was completely hidden and there wasn't any option to display it as a submenu
Then I tried to do something like this:
<item
android:icon="#android:drawable/ic_menu_delete"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_logout">
<menu>
<item
android:id="#+id/menu_logout"
android:title="#string/menu_logout"/>
</menu>
</item>
And it really looks how I want - on a tablet, on a smartphone however, it has an undesired effect - when clicking the Logout option menu, it displays another context menu, which is because of nested menus I believe.
Do you know guys, how can I display a submenu when using the Action Bar on a tablet, and display just a regular option menu on smartphone? Maybe ActionBarCompat requires some additional configuration?
I found a solution.
The solution consists of having 2 separate menu resources files, for tablets and for smartphones: menu_tablet.xml, menu_smartphone.
For tablets, I display a submenu:
<item
android:icon="#android:drawable/ic_menu_delete"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/menu_logout">
<menu>
<item
android:id="#+id/menu_logout"
android:title="#string/menu_logout"/>
</menu>
</item>
For smartphones I display as a regular option menu:
<item
android:icon="#android:drawable/ic_menu_delete"
android:orderInCategory="1"
android:showAsAction="always"
android:id="#+id/menu_logout"
android:title="#string/menu_logout" />
And then in the code I inflate the appropriate menu resource like this:
if(isTablet()){
menuInflater.inflate(R.menu.menu_tablet, menu);
}else{
menuInflater.inflate(R.menu.menu_smartphone, menu);
}
Related
I developed an application and I wanted to have some less common feature under the 3 square button
On android 4.0, I have the result I want : (3 square button, when I click on it, I have the others features)
On android 2.3, the device has a menu physical button but the feature aren't available because they are overrided (the menu button call the sliding menu).
Is it possible to show the 3 square button on all device with action bar sherlock ? and use the menu physical button to show the sliding menu ?
My menu definition :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_download"
android:icon="#drawable/ic_menu_d_download"
android:showAsAction="always|withText"
android:title="#string/download"/>
<item
android:id="#+id/menu_print"
android:icon="#drawable/ic_menu_d_print"
android:showAsAction="withText"
android:title="#string/print"/>
<item
android:id="#+id/menu_share"
android:icon="#drawable/ic_menu_d_share"
android:showAsAction="always|withText"
android:title="#string/share"/>
<item
android:id="#+id/menu_accept"
android:icon="#drawable/ic_menu_d_accept"
android:showAsAction="always|withText"
android:title="#string/ok"/>
<item
android:id="#+id/menu_delete"
android:icon="#drawable/ic_menu_d_delete"
android:showAsAction="withText"
android:title="#string/delete"/>
</menu>
When I see the documentation of android:showAsAction, I didn't find a value that would help http://developer.android.com/guide/topics/resources/menu-resource.html
Regards
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
I have this menu layout
<item
android:id="#+id/refresh"
android:icon="#drawable/refresh"
android:title="#string/refresh"/>
<item
android:icon="#drawable/sort_neutral"
android:title="Sort By Date">
<menu>
<item
android:id="#+id/sortNewToOld"
android:icon="#drawable/sort_up"
android:title="Newest To Oldest"/>
<item
android:id="#+id/sortOldToNew"
android:icon="#drawable/sort_down"
android:title="Oldest To Newest"/>
</menu>
</item>
the items "#+id/refresh" and "#drawable/sort_neutral" are shown correctly, but the two icons inside the "android:title="Sort By Date" are not shown, I just can see the title of them. any help would be appreciated.
note
I am using real mobile song ericsson 2.3.1
Your icon are probably too large for the menu. Make sure you are using a correct icon size.
I'm using ActionBarSherlock to provide a unified UI for my Android 2.x and Android 4.x users. I have a menu with 6 items.
On a 2.x, 480px wide device with an hdpi screen, only 5 of the icons show. The device has a hardware Menu button, but when I tap it, nothing shows up. I expected it to popup and show the Action that wasn't able to fit.
I expect either the three-vertical-dots button to appear in the ActionBar to show a dropdown with the overflowed actions or I expect the physical Menu button to show the old style menu with the overflowed actions.
What am I missing or what am I doing wrong?
Here is my defined menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/add"
android:icon="#drawable/ic_menu_btn_add"
android:showAsAction="always"
android:title="Add"/>
<item
android:id="#+id/calculateNPV"
android:icon="#drawable/menu_icon_npv"
android:showAsAction="always"
android:title="NPV"/>
<item
android:id="#+id/calculateIRR"
android:icon="#drawable/menu_icon_irr"
android:showAsAction="always"
android:title="IRR/YR"/>
<item
android:id="#+id/send"
android:icon="#android:drawable/ic_menu_share"
android:showAsAction="always"
android:title="#string/share_pdf"/>
<item
android:id="#+id/graph"
android:icon="#drawable/ic_menu_gallery"
android:showAsAction="always"
android:title="#string/view_cashflow_diagram"/>
<item
android:id="#+id/deleteReorder"
android:icon="#drawable/ic_menu_clear_playlist"
android:showAsAction="always"
android:title="#string/delete_reorder_cashflows"/>
</menu>
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.