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.
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.
I am currently closely following the steps on the website
https://developer.android.com/training/basics/actionbar/adding-buttons.html and at this stage of the tutorial it wants me to copy and paste the code to add an action search and action settings. However, when I run my application, the action search doesn't want to appear.
I have also made sure to include a .png for the icon of the action search, but still won't show.
I have also tried changing the minimumsdk version in my build.gradle from 8 to 11 as suggested by the website, but didn't work either. However, if I am not mistaken, the action bar is present in the app though since the overflow is there.
From my wild guess, it might be that the code is outdated since I have noticed a lot of things have changed since this tutorial was written. But I am still clueless about this weird problem.
you have to create an entry inside res/menu,override onCreateOptionsMenu and inflate it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.yourentry, menu);
return true;
}
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_cart"
android:icon="#drawable/cart"
android:orderInCategory="100"
android:showAsAction="always"/>
</menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="1"
app:showAsAction="always"
android:icon="#drawable/ic_action_settings"
android:title="#string/action_settings"/>
<item
android:id="#+id/volume"
android:orderInCategory="2"
android:title="Volume"
android:icon="#drawable/ic_action_volume_on"
app:showAsAction="always"/>
You need to xmlns referencing res-auto and then use it as I have used in my code. Hope this helps.
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 am using ActionBarSherlock to create a menu (with the three dots) at the top and then have a few items as a submenu. The menu is showing up on tablets, but not on a four inch phone. The app can run on sdkVersion 9-17.
My menu.xml is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_weather"
android:title="#string/weather"
android:icon="#drawable/weather_tab"/>
<item
android:id="#+id/menu_emergency"
android:title="#string/emergency_nums" />
<item
android:id="#+id/menu_suggest"
android:title="#string/friend" />
<item
android:id="#+id/menu_support"
android:title="#string/faq" />
<item
android:id="#+id/menu_about"
android:title="#string/about" />
</menu>
And I have a menu inflate of:
public boolean onCreateOptionsMenu(Menu menu)
{
getSupportMenuInflater().inflate(R.menu.menu, menu);
return true;
}
Thanks in advance for any help.
In your res/menu/menu.xml, your item can have a android:showAsAction parameter, which defines when to show them. You can then set a bunch of parameters that will help you displaying them (or not) more easily on phones and tablets. If you want to always show the menu item, just set this parameter to "always".
More here :
http://developer.android.com/guide/topics/resources/menu-resource.html
Try
public boolean onCreateOptionsMenu(Menu menu)
{
getSupportMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
I kept searching and this is what finally worked for me: https://stackoverflow.com/a/11438245/2291915
I hope this can help someone else. I hadn't realized because I was using this on an emulator it was really about whether there was hard or soft buttons.
Sometimes when doing some so very simple, you miss something big. I must be missing something huge because I am getting nowhere fast (and an hour of sifting through the web has revealed nothing).
I want to have a menu with items with checkmarks in group--just like a simple RadioGroup layout. I get the menu, but no checkmarks of any kind.
Here's the res/menu/options_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:checkableBehavior="single">
<item
android:id="#+id/item1"
android:title="item1"
/>
<item
android:id="#+id/item2"
android:title="item2"
/>
<item
android:id="#+id/item3"
android:title="item3"
android:checked="true"
/>
</group>
</menu>
And of course, here's the relevant methods in my Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
This is about as simple as I can make it--any ideas on what I'm missing?
I think you try to create Options Menu (it's displayed by click menu button) and according to this
Note: Menu items in the Icon Menu (from the Options Menu) cannot
display a checkbox or radio button. If you choose to make items in the
Icon Menu checkable, you must manually indicate the checked state by
swapping the icon and/or text each time the state changes.
you can't add group menu in OptionsMenu. so i think you shoud use Context Menu or Submenu.
Take a look at this article
Here's the work-around:
In your strings.xml file you can embed a unicode checkmark. There are two to choose from. For this project I prefer the friendlier check of \u2714. You then swap a string with the check-mark for a string without it via onPrepareOptionsMenu() as appropriate.
Here's the xml code two strings, one with and one without checkmarks:
<string name="opp_random">unchecked</string>
<string name="opp_random_check">\u2714 checked</string>
Happy coding!
How about
android:checkable="true"