Im using appcompat v7 for rendering the action bar on my Android project. This works very well on versions 4.x but the options menu (only the options menu) is not exibithed on 2.x.x versions. What is the problem?
My list_team.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_profile"
android:title="#string/menu_label_profile"
android:orderInCategory="100"
android:showAsAction="never"
app:showAsAction="never"/>
<item
android:id="#+id/action_about"
android:title="#string/menu_label_about"
android:orderInCategory="100"
android:showAsAction="always"
app:showAsAction="always"/>
My Activity
public class MyActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_team);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list_team, menu);
return true;
}
}
Appreciate any help.
Try to delete these two attributes android:showAsAction="" and keeping only these app:showAsAction="" instead.
According to the Google Documentation:
If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace.
You can also change the orderInCategory of *action_about* to 1.
Based on this code, you are missing super.onCreateOptionsMenu() in your override:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list_team, menu);
return super.onCreateOptionsMenu(menu);
}
And you should also only use app:showAsAction namespace as has been previously suggested.
Devices with Android 2.X usually have a hardware menu button. In that case the overflow menu icon is not displayed (at least by default, not sure if there is a toggle for this in appcompat, though there was in ActionBarSherlock).
Nevertheless, tthe options should be accessible when pressing the menu button. May that be the case?
Related
I want my refresh item to appear on the top bar. I am extending ListActivity and using the theme android:Theme.Holo.Light.DarkActionBar. The menu is created using:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="eu.pcas_project.client.android.pa.services.ServiceList">
<item
android:id="#+id/refresh_all_services"
android:title="#string/refresh_all_services"
android:icon="#drawable/ic_menu_refresh"
app:showAsAction="always" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_service_list, menu);
return super.onCreateOptionsMenu(menu);
}
If I change app:showAsAction="always" on the menu XML to android:showAsAction="always", this works as I want it to—icon on top bar—but then I get an error saying Should use android:showAsAction when not using the appcompat library. Can it be fixed?
Targeting API 19.
It depends, if you hav a physical button for menu it doesn't appear. but you can try this:
<item
android:id="#+id/refresh_all_services"
android:icon="#drawable/ic_menu_refresh"
android:title="#string/refresh_all_services"
app:showAsAction="withText|ifRoom"
app:actionProviderClass="android.support.v7.widget.ActionProvider"
/>
As said in the question, android:showAsAction="always" works but generates an error.
While the project was not using the appcompat library directly, I discovered that a library imported by said project had com.android.support:appcompat-v7:XXX.YYY.ZZZ as a dependency even though it was not needed. This was added by Android Studio. Once the line was removed and the project rebuilt the error disappeared and the refresh icon appeared on the top right corner instead of on the menu.
I am debugging on a Nexus, Android version 5.0
My Min SDK is 11, target SDK is 21.
I have the following XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/settings"
android:title="#string/settings_label"
app:showAsAction="ifRoom"/>
</menu>
And in my launcher activity I have this Java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i("Inside onCreateOptionsMenu", "True");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_start, menu);
return true;
}
However that Log line never makes it into LogCat, and my menu is never displayed.
My desired effect is to have an action bar with the 3 vertical dots which when clicked by the user will show my menu item.
You can go through the tutorial from several different providers as follows which are recommended by SO
Android Developer
Vogella
AndroidHive
All of them have great examples and source code to help you out
I can't understand why wrong and incompatible (AndroidStudio tells me "Should use app:showAsAction with the appcompat library) code
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/search"
android:showAsAction="always" />
</menu>
works perfect, but proper and compatible version like
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/search"
app:showAsAction="always" />
</menu>
not showing my icon at all.
I'm testing on Samsung GT P5210 (android v. 4.4.2) and Nexus 7 (4.4.4)
Things you should always check when you want to use action bar are
1) Extend ActionBarActivity instead of Activity
public class MainMenu extends ActionBarActivity{
2) Have the right style selected as defined at manifest
Manifest
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
Style
<style name="AppTheme"
parent="Theme.AppCompat.Light.DarkActionBar">
</style>
3) Select the right title for showAsAction
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:**yourapp**="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
**yourapp**:showAsAction="ifRoom" />
...
</menu>
This is what most people get wrong
4) Define your Menu in Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
If you do all the following your action bar should work.
Then you should add the onClickListener for every position...
I just reread your question and saw your problem is the complete opposite (but some pieces of my old answer still apply to your problem), so here is the updated answer:
Update:
You have imported the appcompat library in your gradle file, but you seem to support only devices newer than API Level 11 or 14? If this is the case, 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. But as your android:showAsAction attribute is working, you are using the native Activity and the native attribute call is correct, even if the lint check says it is wrong. So if you want to remove the lint error, you have to delete the appcompat lib from your gradle file and maybe change your activity theme to the native Holo Light theme, as your current theme might rely on the appcompat theme.
The answer why it isn't working with the app namespace is in the XML attribute loading for native respectively library code, which is handled in the old answer.
Old Answer
If you are using the ActionBarActivity from the support library to reach devices lower than API level 11, the main issue here is, that the ActionBarActivity recreates some of the native Android XML attributes such as android:showAsActionin its own scope, which you define with:
xmlns:app="http://schemas.android.com/apk/res-auto"
and then access them with the same attribute (here showAsAction) in the app: namespace.
So the ActionBarActivity can't see or reach the native android:showAsAction attribute as it is only looking for it in the app namespace and not the android namespace.
If you want to use the native attribute, you have to use the native Activity with a Holo Theme, which is only supported from API Level 11 and higher.
addthis:
yourapp:showAsAction="ifRoom"
or
android:showAsAction
for example:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
</menu>
and in Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And read more here
I am trying to display an options menu in a certain fragment in my android application.
In my fragment i have overridden the following methods:
#Override
public void onPrepareOptionsMenu(Menu menu){
getActivity().invalidateOptionsMenu();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.overview_activity_menu, menu);
}
The methods are triggered when a user presses the physical options-button of an android device (I am not using an actionbar).
The menu is specified like this.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_add_as_dish"
android:showAsAction="ifRoom"
android:title="#string/action_add_as_dish" />
<item
android:id="#+id/action_copy_to_other_day"
android:showAsAction="ifRoom"
android:title="#string/copy_to_other_day" />
<item
android:id="#+id/action_add_to_shoppinglist"
android:showAsAction="ifRoom"
android:title="#string/add_to_shoppinglist" />
<item
android:id="#+id/action_remove"
android:showAsAction="ifRoom"
android:title="#string/remove_selected" />
</menu>
Now, this code works well on a Samsung Galaxy S4 API level 18 (I´ve tested using Genymotion) and the menu is displayed exactly like I want it.
However, when I run this code on my own devices, the menu just won't show.
I have used breakpoints to verify that the methods are actually called, but there is no menu visible.
My own devices run on API <= 16, and I am sensing that this has something to do with why the menus are not displayed.
How can I make the menu appear on API >= 14 devices?
P.S. API 14 is the min. SDK version that I need to support.
Should be simple enough but might not be.
When using action bar in Android 3.0+ you have the option of defining your menu items in XML or in code. I prefer to code them in xml as action bars feel more UI based than functional.
On an average day, you would use this to inflate the xml into a menu
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Menu is defined inside 'res/menu/...xml
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
And your XML file would look like this
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/menu_settings"/>
<item
android:id="#+id/menu_item_menu"
android:icon="#drawable/menu_off_128"
android:showAsAction="ifRoom|withText"
android:title="#string/inbox_string"/>
<item
android:id="#+id/menu_item_gallery"
android:icon="#drawable/gallery_off_128"
android:showAsAction="ifRoom|withText"
android:title="#string/gallery_string"/>
<item
android:id="#+id/menu_item_inbox"
android:icon="#drawable/inbox_off_128"
android:showAsAction="ifRoom|withText"
android:title="#string/inbox_string"/>
<item
android:id="#+id/menu_item_contact"
android:icon="#drawable/phone_off_128"
android:showAsAction="ifRoom|withText"
android:title="#string/contact_string"/>
</menu>
Right now, I'm faced with the problem of making an actionbar backwards compatible and actionbarsherlock seems to be the most pleasant to use and popular.
So I tried the above with actionbarsherlock and sadly there are compile time issues.
Namely that the Menu class returned by the inflater is from 'Android.view.menu' and not the 'com.actionbarsherlock.menu'. I went digging through the samples on github but all of them have the menu defined in code.
So has anyone manged to get an actionbarsherlock menu working with an XML file based layout?
try this
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getSupportMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
Just had this problem myself.
What you want to to do is call getSupportMenuInflater() instead of getMenuInflater() like so:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}