My original application was written for Android 2.1. Afterwards, I've added compatibilty library and ActionBar Sherlock.
Now, I would like to present options menu as overflow in the action bar and it works as expected. However, on devices without menu button, I still get default menu bar at the bottom of the screen. Clicking on it opens the options menu from the action bar. See image below:
What is worse, this bar shows even on activities that have no options menu defined.
Regarding my relevant code, there is nothing special about it.
Inflating options menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:icon="#drawable/ic_menu_flag" android:title="#string/I_STR_LANGUAGE" android:id="#+id/menu_lang" android:showAsAction="never"></item>
<item android:icon="#drawable/ic_menu_pin_change" android:title="#string/change_pin" android:id="#+id/menu_pin_change" android:showAsAction="never"></item>
<item android:icon="#drawable/ic_menu_about" android:id="#+id/menu_about" android:title="#string/about_application" android:showAsAction="never"></item>
<item android:icon="#drawable/ic_menu_exit" android:id="#+id/menu_logout" android:title="#string/I_CLOSE" android:showAsAction="never"></item>
</menu>
Application theme inherits from DarkActionBar Sherlock Theme
<style name="AppTheme" parent="Theme.Sherlock.Light.DarkActionBar">
Is there a way to hide system menu bar? Can options menu be presented only from action bar? Can it at least be hidden for activites without options menu?
[UPDATE:] <uses-sdk android:minSdkVersion="7" />
You need to modify your uses-sdk node to target the latest API, like so :
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18"/>
Related
My ActionBar has 2 action buttons, when I'm trying to change logo on my ActionBar, it doesn't show. I found the solution: in my Mainactivity class I changed ActionBarActivity(it shows by default) to Activity. Then in manifest.xml I change the Theme from
name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to
name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
the logo appeared, but 2 action buttons moved to overflow menu. The question is: How to move this action buttons back?
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=".MainActivity">
<item android:id="#+id/action_share"
android:title="#string/action_share"
android:icon="#drawable/ic_action_share"
android:orderInCategory="110"
app:showAsAction="always" />
<item android:id="#+id/deleteNote"
android:title="#string/delete"
android:orderInCategory="111"
android:icon="#drawable/ic_action_delete"
app:showAsAction="always" />
</menu>
MainActivity
#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);
if (isAddingNote)
{
menu.removeItem(R.id.deleteNote);
menu.removeItem(R.id.action_share);
}
return true;
On this forum I found two close posts but it didn't help me.
Action buttons doesn't show up on Action Bar?
and Actionbar not shown with AppCompat
As per my understanding to your question you were originally trying to change the icon of the actionBar in which you didn't succeed and end up with other error.
Let me tell you that your menu_main.xml code works perfectly there is no error in that.
There are two possible solutions for you and that depends on your requirements.
If you are developing your app with material design support i.e using Appcompat then use following answer.
Change android:Theme.Holo.Light.DarkActionBar to Theme.AppCompat.Light.DarkActionBar again.
Change extends Activity to extends ActionBarActivity again.
Write this code in your java file to change the logo of the actionbar.
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setIcon(R.drawable.YourLogo);`
If you want to implement your app with Holo theme then use following answer.
First of all remove Appcompat lib from your project depending on the IDE you are using.
Let your activity be extends Activity.
Let your theme be android:Theme.Holo.Light.DarkActionBar.
Change TargetSDK to below 21. Change compile version to below 21.
Replace you main_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_share"
android:title="share"
android:orderInCategory="110"
android:showAsAction="always"
android:icon="#drawable/ic_action_share" />
<item android:id="#+id/deleteNote"
android:title="delete"
android:orderInCategory="111"
android:showAsAction="always"
android:icon="#drawable/ic_action_delete" />
</menu>
P.S. I do not understand why you removing menu items from your code.
I.e.
if (isAddingNote)
{
menu.removeItem(R.id.deleteNote);
menu.removeItem(R.id.action_share);
}
In the new update Google has released a new API support library, that supports the ActionBar in API level 7+.
I used ActionBarSherlock until this update and I wrote the code to load the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true;
}
and the menu file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item_menu_ok" android:icon="#drawable/ic_action_ok"
android:title="#string/ok" android:showAsAction="always"></item>
<item android:id="#+id/item_menu_cancel" android:icon="#drawable/ic_action_cancel"
android:title="#string/cancel" android:showAsAction="always"></item>
</menu>
To set up the menu buttons on the action bar. This code worked perfectly with ActionBarSherlock. But when I changed the action bar to the new support library, the buttons are not shown in the action bar. Even if they are set as android:showAsAction="always". And when I debug the code, the function menu.getSize() return 2, and that is correct, but no buttons are shown..
Why are the buttons not shown in the new support library?
Try pressing the MENU button on your device or emulator, and see if they appear in the overflow.
If they do, then the problem is that your <menu> XML needs to change. Menu XML that works with ActionBarSherlock and the native API Level 11+ action bar will not work with the AppCompat action bar backport.
Your menu XML would need to look like this:
<?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/item_menu_ok" android:icon="#drawable/ic_action_ok"
android:title="#string/ok" yourapp:showAsAction="always"></item>
<item android:id="#+id/item_menu_cancel" android:icon="#drawable/ic_action_cancel"
android:title="#string/cancel" yourapp:showAsAction="always"></item>
</menu>
And you would need to use the same yourapp prefix for anything else related to the action bar (e.g., yourapp:actionLayout).
You can see this covered in the action bar documentation.
I'd like to add a little to the answer.
If you want to see both text and an icon, please use withText in showAsAction
I've just tested it; when I used always or ifRoom without withText, I only saw an icon.
I'm using ActionBarSherlock and I'm trying to add an options menu to the ActionBar.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_prev"
android:title="#string/menu_previous"
android:icon="#drawable/left_navigation"
android:showAsAction="always|withText" />
<item android:id="#+id/menu_next"
android:title="#string/menu_next"
android:icon="#drawable/right_navigation"
android:showAsAction="always|withText" />
</menu>
The icons are 32x32 pixels in the drawable-mdpi directory. When the ActionBar is split (portrait orientation), the icons show up but no text shows (plenty of room though). Long-pressing the icon Toasts the title. When the ActionBar is not split (landscape orientation), both the icons AND text show up as expected.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
Menu, MenuInflater, and getSupportMenuInflater() are all using the ActionBarSherlock implementations.
The 'Application' in the manifest declares:
android:uiOptions="splitActionBarWhenNarrow"
What can I do to make it show the title of each item when in portrait orientation?
Edit: Possibly relevant is that I'm running Android 4.0.4.
If you are using Android >= 3.0 ABS uses the system implementation of the actionbar, thus the behavior you see is correct. See #CommonsWare comment to the question as to why the behavior is that way.
I am developing Android 2.1 API 7 app. To implement action bar, I am using ActionbarSherlock library.
Everything goes fine with the sherlock library, I can implement action bar with it in my project with the following code.
res/menu/action_menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_payment_1"
android:title="#string/new_payment"
/>
<item
android:id="#+id/label_1"
android:icon="#drawable/ic_launcher"
android:showAsAction="always"/>
<item
android:id="#+id/label_2"
android:title="text2"
android:showAsAction="always"/>
<!-- overflow section of action bar -->
<item android:title="title2"/>
<item android:title="title3"/>
<item android:title="title4"/>
</menu>
In my Activity class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
I got action bar successfully with above code. No problem at all on Android 3.2 platform.
BUT the problem is if I run my app on Android 2.1 platform, the action bar has no overflow section on the Action Bar. Why??? Anyone has experienced the same problem when using Sherlock library on old Android platform??
(P.S. "overflow section" of action bar is the right-most part of action bar which hides some items like a popup menu. More info here )
It uses the native options menu as overflow, just as an Ice Cream Sandwich phone would should one be made with a hardware menu key.
Forcing an overflow action item to be on the action bar on pre-4.0 devices will be a feature of version 4 of ActionBarSherlock.
Support for overflow menu for pre-Ice Cream Sandwich devices has been removed from ActionBarSherlock. A good discussion of this can be found here: Force overflow menu in ActionBarSherlock
If I define the following items for my action bar:
res/menu/action_menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="label"/>
<item android:title="label1"/>
<item android:title="label2"/>
<item android:title="label3"/>
<item android:title="label4"/>
</menu>
In my Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
Is there anyway to allow me define certain items move to action overflow part ? and how to do it?
P.S. Action overflow part is the right-most part of action bar which hide certain items like a popup menu.
It's the other way round. You need to explicitly tell the menu which ones you want in the ActionBar and which not by setting the appropriate flags
E.g.
<item android:id="#+id/refresh"
android:title="#string/refresh"
android:icon="#drawable/reload_button"
android:showAsAction="always"/>
Here android:showAsAction tells how to handle it. Options are
always
ifRoom
never
withText
You can or options together with the pipe symbol as "always|withText"
See the android docs for action bar for more documentation.
To add something to Heiko's answer about the "overflow menu" on the action bar, this only happens if you have items set as ifRoom and there is no room for them to be displayed. On the overflow menu they only appear with a title and no icon.
On Android 4.0, the overflow menu ("3 dot spinner") is only shown on devices that don't have the physical "menu" button. You can test this on an ADV setting the option Hardware Back/Home keys option to "no".