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.
Related
Android Studio 0.5.8
Hello,
For some reason the icon never displays on the ActionBar, I have used a combination of ifRoom|withText but still doesn't display. I have also tried rotating in Landscape. I am using genymotion 4.4.2
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="#string/new_crime"
android:id="#+id/menu_item_new_crime"
android:icon="#drawable/ic_action_new"
app:showAsAction="always"/>
</menu>
I am inflating the menu in a fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_crime_list, menu);
}
Here is a screenshot:
I have tried hardware nexus5 in portrait and landscape mode, but no icon.
I have also tried using the following, but didn't work either:
android:icon="#android:drawable/ic_menu_add"
Many thanks for any suggestions,
I have come across this issue once myself. Try this:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="#string/new_crime"
android:id="#+id/menu_item_new_crime"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"
app:showAsAction="always"/>
</menu>
I don't know why it would be necessary to have both, but that fixed it for me for some reason.
You are using Android Studio, as explained here by blackfizz: "the lint check sees that you have imported the appcompat library via gradle and it thinks that you should use the ActionBarActivity because of your library import. That's why you are getting the error."
I had the exact problem. Android Studio was giving me the error "should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto". If I changed my XML as suggested, my menus in the actionBar disappeared into the overflow. If I ignored the error, I got the expected behavior, but the error still bothered me.
The real culprits turned out to be the following lines in the file build.gradle:
dependencies {
…
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:support-v4:22.1.1'
}
which imported the appcompat library, and caused all the trouble. Since I only targeted Android 4.4 and up, I was able to remove these two lines. Problem solved!
I wasted a few hours to figure it out myself before reading blackfizz's answer, so I am posting my answer here in hopes of saving other developers a few hours.
When you encounter a similar situation, first check your build.gradle to see if you have inadvertently imported the appcompat library.
You need to use Theme.Holo style and not AppCompat.
In order to do so, just change the style of the application at AndroidManifest.xml
If you get error:
should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto
Then you need to change the module settings:
1 - Right Click on your app and Select Open Module Settings (Or just Press F4)
2-In the depencencies, add a support module newer than V7 (for example com.android.support:support-v13:22.0.0)
in the menu.xml, dont write:
app:showAsAction="ifRoom"
but write
android:showAsAction="ifRoom"
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();
When using ActionbarCompat as Actionbar BackPort I am having the problem that action-icons do not show up - same code/res works with actionbarsherlock.
Am I doing something wrong or is this not yet supported? I am also missing the whole Menu/MenuItem getSupportMenuInflater() part that ABS has in ActionBar compat - can anyone shed some light on this?
This question was already answered in Actionbar not shown with AppCompat.
Add the following namespace to the "menu" item in your xml file
xmlns:compat="http://schemas.android.com/apk/res-auto"
Then change the "showAsAction" attribute to use the new namespace
compat:showAsAction="ifRoom"
Here's a full example with one item in the menu, with the changes on lines 2 and 6 (from Actionbar not shown with AppCompat)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_whatever"
android:icon="#drawable/ic_action_whatever"
android:title="#string/whatever"
compat:showAsAction="ifRoom" />
</menu>
When using the new ActionBarActivity, you no longer need getSupportMenuInflator. Your code should look like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
When calling invalidateOptionsMenu with ActionBarActivity you do need to use the new support version:
supportInvalidateOptionsMenu();
Those are the only two main differences between ActionBarSherlock and the new ActionBarActivity that I have found.
I am using the actiobarcompat sample in my application and I am trying to implement search for pre 3.0 devices.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:orderInCategory="1"
android:title="#string/menu_search"
android:icon="#drawable/ic_home"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
On Honeycomb+ this works fine, the searchview widget appears in the actionbar. What I am trying to do is have a second menu XML so I can fall back to the old search activity way of doing it. However, there is no such thing as menu-v11 folder as the menu folder is essentially menu-v11 because that is the version it started supporting this.
My question is, using the actionbar compatibility sample, is there a way to declaratively add an alternate button for pre-honeycomb?
Can you please be more specific about what you are trying to achieve?
It it's about calling different activities depending on API version, the action bar has nothing to do with that.
You analyse API version in onOptionsItemSelected and act accordingly.
If you want different menu items depending on API version, just create a folder menu-v11 (or menu-v14) and put
version-specific xml-s- there.
BTW, I use com.android.actionbarcompat and it works great for me!
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.