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.
Related
I'm trying to implement a menu in a ListActivity. Here's how I'm declaring the menu item:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/camera"
android:title="#string/camera_name"
android:icon="#drawable/cam"
app:showAsAction="always" />
</menu>
And this is what the preview shows in Android Studio:
The preview suggests everything is fine. It shows the camera icon that I put in the res/drawable folder.
Yet, when I try to run it in an emulator (Nexus 4 API 22), this is how the app shows up:
So the actual emulator is pushing the icon into the overflow menu despite being set as showAsAction="always".
This is how I am inflating the menu in the ListActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
Does anyone know what I'm doing wrong? Thanks in advance.
It is possibly because you have used app:showAsAction. So if you are using support library, simply add android:showAsAction beside app:showAsAction. This have to solve your problem.
In my Android Application I want to have a options menu in the toolbar and the functionality works just fine. However it takes a considerably long time (about one second) until the menu show after the 3 dots in the toolbar are pressed.
At first i thought, that it might be some problem specific to my app, but I created a new project from the Android Studio template and the problem still persists. (Though it feels a little bit faster than in my application)
The menu is created as documented in the docs:
menu.xml
<?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:id="#+id/manageBTConnection"
android:icon="#drawable/ic_bluetooth_white"
android:orderInCategory="100"
android:title="#string/connect"
app:showAsAction="ifRoom" />
<item
android:id="#+id/backgroundService"
android:title="Start background service" />
<item
android:id="#+id/sendToBackend"
android:title="Send to Backend" />
</menu>
MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
Compared to other Android apps it takes a lot longer than usual and feels clunky.
Is there any way to speed up the creation/showing of the dropdown menu?
Many thanks!
I'm experiencing the same thing. Here I've profiled what happens when I tap to open the menu. Notice that it takes 900 to 1000 milliseconds for the menu to appear.
After updating the compileSdkVersion and targetSdkVersion level to 26, on tablet devices with API level 20 to 25, submenus get shown at the center of the Toolbar (or ActionBar), not next to the menu.
Screenshots for a better understanding:
Expected result, submenu next to the menu
Actual result, there's a gap between the submenu and the menu
I assume it's a bug within the Support library 26? When targeting Api level 24 this issue doesn't occur on any device.
Has anyone also experienced this issue and maybe a fix?
Code from a test project:
The menu XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:emma="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_send_mail"
android:icon="#null"
android:orderInCategory="1"
android:title="send"
emma:showAsAction="ifRoom"/>
<item
android:id="#+id/menu_save_draft"
android:icon="#null"
android:orderInCategory="2"
android:title="save"
emma:showAsAction="never"/>
The onCreateOptionsMenu method
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.message_compose, menu);
SubMenu debugSub = menu.addSubMenu("More actions");
debugSub.add("Action 1");
debugSub.add("Action 2");
return super.onCreateOptionsMenu(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
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?