Android ActionBar compat overflow menu not showing on sdk 10 - android

Hello and thank you for the time you take in reading this question.
I am trying to develop an android app which will use the ActionBar compat library. I have followed (as far as I see it) all the recommendations when using the compat library. My Manifest looks like this(only relevant code shown):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
</application>
</manifest>
As you can see I am targeting sdk 8+. I have used the Theme.AppCompat theme as recommended.
My menu file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cds="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_map"
android:icon="#drawable/ic_action_map"
android:title="#string/action_map"
cds:showAsAction="ifRoom"/>
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
cds:showAsAction="ifRoom"/>
<item
android:id="#+id/action_mail"
android:icon="#drawable/ic_action_mail"
android:title="#string/action_mail"
cds:showAsAction="ifRoom"/>
</menu>
I am using my own namespace for the showAsAction attribute.
My activity extends the ActionBarActivity class.
The problem is this: On sdk 10 (android 2.3.3), both on device and emulator, the overflow menu (the three dots on the right side of the action bar) are not shown. Only the first 2 of the menu items are shown on the action bar. If i press the "Menu" button on the device then the third item is shown from the bottom left corner of the screen (not from the upper right corner as on the devices with more recent android versions). The same code works well on android sdk 17 on emulator (the overflow menu is shown with the proper actions).
I have searched the web for a solution but I could not find one with this specific problem. I would have abandoned the issue if I wouldn't have installed apps on the android 2.3.3 device that have the same action bar and which show the overflow menu icon and work properly like on any recent android device. One example of this app is the todoist app (https://en.todoist.com/android) or the handcent app(https://play.google.com/store/apps/details?id=com.handcent.nextsms&hl=en) which both behave well on this device.
Is there anything I am missing or is there an alternative solution to the recommended way of using the actionbar compat?
Thank you for your time.

#Andrei Google have disabled the menu overflow button in appcompat on pre honycomb.
If You really want to add it go to the android's github repository and download
platform_frameworks_support. It contains sorce for appcompat in platform_framework_support_master/v7/appcompat.
Create a libs folder inside appcompat and put latest android-support-v4.jar.
Now open file v7/appcompat/src/android/support/v7/internal/view/ActionBarPolicy.java.
You will see that showOverflowMenuButton is returned false for pre honycomb.Just return it true and add this edited appcompat as library to your project
and you will not need any custom overflow button
This worked with me.
Sorry for my English
EDIT: actual code from android/support/v7/internal/view/ActionBarPolicy.java
public boolean showsOverflowMenuButton() {
// Only show overflow on HC+ devices
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}

Try to show as I did.
I add overflow menu (three dots) manually:
<item
android:id="#+id/menu_more"
android:icon="#drawable/abc_ic_menu_moreoverflow_normal_holo_light"
android:title="#string/action_settings"
myapp:showAsAction="always">
<menu>
<item
android:id="#+id/submenu_about"
android:showAsAction="always"
android:title="#string/menu_about"/>
</menu>
</item>
and override menu button click in activity to show this menu (solution from Opening submenu in action bar on Hardware menu button click):
private Menu mainMenu;
...
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
mainMenu.performIdentifierAction(R.id.menu_more, 0);
return true;
}
}
return super.onKeyUp(keyCode, event);
}
Result on 2.2 looks like:
Hope it helps you.

I found the answer finally for this situation.
All you need to do is call the following in the OnCreate in your activity
ActionBarPolicy.get(this).showsOverflowMenuButton();

Related

Android drawable icon not appearing on 4.4.2

I have an Android app on the play store (Raleigh Nights) and it has just been brought to my attention that the drawable icons in my overflow button are not showing, although the text is showing properly. Everything is showing properly on 4.3 and I have no idea what may cause the difference. When I debug using an emulator it seems to set MenuItem icon and doesn't throw any exceptions. I've spent hours trying to figure out what is going on to no avail. Again, it works in older versions, but does not show the icon in 4.4.2 (KitKat). It also crashes on some 4.4.2 phones although I can't get it to crash on the emulator.
I have the target set to 19.
minVersion = 11;
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
I'm also going to include the menu button that I have to see if that helps.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Will always be in the overflow -->
<item android:id="#+id/menu_drink_specials"
android:title="#string/drinks"
android:icon="#drawable/added"
android:showAsAction="never"/>
<item android:id="#+id/menu_food_specials"
android:title="#string/food"
android:icon="#drawable/added"
android:showAsAction="never"/>
<item android:id="#+id/menu_events"
android:title="#string/events"
android:icon="#drawable/added"
android:showAsAction="never"/>
<item android:id="#+id/sort_location"
android:title="#string/sortLocation"
android:icon="#drawable/added"
android:showAsAction="never"/>
</menu>
Has anyone else run across this problem? It seems odd that it works so well in the the other versions and isn't throwing any errors up.
Thanks for your time,
Mike
Are you certain that you have icons showing next to items in the overflow menu? This is intentionally not allowed:
Displaying icon for menu items of Action Bar in Honeycomb android 3.0
It seems like there are several hacks to make something like this work. Maybe you're using one of them and it's what's causing your crash.
However if you want full control over this, it may be best to extend PopupWindow and simply inflate whatever layout you'd like into it. You could then create a 'fake' overflow button in the action bar and configure the PopupWindow to display beneath it.
The way you have your items set up with android:showAsAction="never" will never put icons into the overflow menu. Android, by default, does not allow this. The only way to display icons is to make showAsAction equal to always or ifroom and also have android:icon set. Your app will most likely look fine without the icons in the overflow menu.
Try to put this code on your activity.
#Override
public boolean onMenuOpened(int featureId, Menu menu)
{
if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
Log.e(TAG, "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
add these attributes to the menu tag and try
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
You configured "android:showAsAction="never"" and this way and will never display the icons into the overflow menu.
Try change your code like this
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Will always be in the overflow -->
<item android:id="#+id/menu_drink_specials"
android:title="#string/drinks"
android:icon="#drawable/added"
android:showAsAction="ifRoom"/>
<item android:id="#+id/menu_food_specials"
android:title="#string/food"
android:icon="#drawable/added"
android:showAsAction="ifRoom"/>
<item android:id="#+id/menu_events"
android:title="#string/events"
android:icon="#drawable/added"
android:showAsAction="ifRoom"/>
<item android:id="#+id/sort_location"
android:title="#string/sortLocation"
android:icon="#drawable/added"
android:showAsAction="ifRoom"/>
</menu>

Android menu item showAsAction="always" doesn't work

I have tried setting the:
android:showAsAction=".."
to every one of these:
ifRoom, never, withText, always, collapseActionView
but I always got the same result, which is not having any buttons on the action bar, so I have to press the 'menu' button.
Here is a picture of the menu now :
<item android:id="#+id/smth1"
android:title="#string/smth1"
android:showAsAction="always"
android:orderInCategory="1" />
I have even tried adding this:
android:uiOptions="splitActionBarWhenNarrow"
into application manifest file, but with no positive result (nothing changed).
I have tried running it on various kind of APIs (14, 16, 17, 19), but with the same result.
If my question seems to be unclear, here is a picture of a menu, which I would like to have:
Thanks for any help.
You need to use the compatibility namespace (see here)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="always"/>
</menu>
Once you're using that you can use as many menu buttons as will fit. You're no longer limited to just two buttons + overflow showing.
You maybe just don't have enough space, you should first remove the title from your Activity by adding this to your onCreate method:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
Then you can see here that the maximum number of icons you can display is directly linked to the width of your screen, on small phones you will only be able to see 2 icons in the ActionBar.

Display action bar menu button

Is there any possible way to display the three dots menu in action bar on divices with hardware menu button? Like Samsung Galaxy etc.
Looks like your answer is at the link below. There's an explanation of why you wouldn't want to do it, and another answer providing a hack to do it anyway (on android 4.x devices).
How to force use of overflow menu on devices with menu button
The guide says:
…
Menu items that are not promoted to an action item are available in the overflow menu, revealed by either the device Menu button (when available) or by an "overflow menu" button in the action bar (when the device does not include a Menu button).
In my app I have nested menu like:
//menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/three_dots_item"
android:icon="#drawable/ic_three_dots"
android:title="#string/title"
android:showAsAction="always">
<menu>
<!-- items to show -->
</menu>
</item>
</menu>
It's not real overflow, but it's a simple workaround.
This is worked for me
Use
<uses-sdk
android:minSdkVersion="8"
/>
instead of
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
in manifest xml

ActionBarSherlock, ActionBar and Native menu, how to proceed?

I'm writing an android application (sdk 8 to 15) and I'm dealing with the menu. I read a lot of things on it to understand, but I 'm stuck on one point :
Apparently since sdk 10, hardware menu button isn't supported, now we have to use the action bar. Ok. But in my app I want to have this kind of menu :
Or this :
... which are not hardware menu button but seems to be 'native action bar'. All I can find on the web is an action bar on the top, with the title of the app and a logo on the left.. which take a lot of place.
I'm so lost with all those sdk versions and I just want to integrate a simple menu like above, how should I proceed ?
I have this in my code, but :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_fragments_slider, menu);
return true;
}
... but no menu appears with android 4 as it should be in the first or second image.
Check out the Menu Resource in the Android API Documentation and especially:
android:showAsAction=["ifRoom" | "never" | "withText" | "always" |
"collapseActionView"]
The example XML from the documentation is a good resource also for exactally how to lay it out:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item1"
android:title="#string/item1"
android:icon="#drawable/group_item1_icon"
android:showAsAction="ifRoom|withText"/>
<group android:id="#+id/group">
<item android:id="#+id/group_item1"
android:onClick="onGroupItemClick"
android:title="#string/group_item1"
android:icon="#drawable/group_item1_icon" />
<item android:id="#+id/group_item2"
android:onClick="onGroupItemClick"
android:title="#string/group_item2"
android:icon="#drawable/group_item2_icon" />
</group>
<item android:id="#+id/submenu"
android:title="#string/submenu_title"
android:showAsAction="ifRoom|withText" >
<menu>
<item android:id="#+id/submenu_item1"
android:title="#string/submenu_item1" />
</menu>
</item> </menu>
You then need to add to your Manifest:
android:uiOptions="splitActionBarWhenNarrow"
That will move your buttons down to the bottom, leave the title in the actionbar and if you have more than what can fit in the screen you get the little three dot | on the right of the lower actionbar. This only really works in Portrait mode though, unless you have quite a few menu items... or longer text strings (I think...)
ActionBarSherlock puts your menu stuff in the ActionBar across all supported platform versions.
Add it as library project and make sure that you import the Sherlock Menu and extend SherlockActivity.

Android ActionBar isn't created

I'm currently working on a mp3 library on Android. Thing is, there are 4 tab, representing Songs, Albums, Artists and Playlists. My main activity creates a tabspec for each tab and add them to a tabhost.
The problem is, I can't use the ActionBar. I tried the tutorial from Android Developpers and it works fine, however when I try to apply it on my project it doesn't work. I can't see the actionbar and if I try a getActionBar() in my activity it returns null.
As in the tutorial I put this code in the activity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbar_menu, menu);
return true;
}
And I created this menu :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_save"
android:title="Hello !"
android:showAsAction="ifRoom" />
</menu>
I suppose there are more things to do in order to display the actionbar, yet I can't find an accurate on the Web since now ... Does this have something to do with the xml from my main activity ?
Thanks for your attention
For me I added this to my manifest:
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" >
Hopefully that saves some time from having to look it up.
The action bar is only available from Android 3.0 and above, and you need to set your application theme to Holo.
Besides setting the sdk version, for instance:
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="15"/>
You also have to make the application theme use Holo.
make android:minSdkVersion="14" it works for me.

Categories

Resources