Android onCreateOptionsMenu is not called - android

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

Related

Actionbar icon not showing despite showAsAction="always"

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.

SubMenus on tablets get shown at wrong position when targeting API level 26

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);
}

How to get menu options in actionbar aligned with fragments?

On a tablet in landscape, the GMail app has the following:
The magnifying glass menu item appears to be aligned to the right side of the list fragment.
How can I achieve this in my own app?
Step I: Create a xml in the res/menu folder.
Step II: Add the following code snippet in the menu_main.xml or the pre-provided menu.xml
<?xml version="1.0" encoding="utf-8"?>
<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:title="#string/action_search"
android:icon="#drawable/ic_action_search_hdpi"
yourapp:showAsAction="always"
android:enabled="true"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
`
Step III: Inflate the menu.xml in the onCreateOptionsMenu(Menu menu)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.menu_main, menu);
return true;
}
Since Search is also a menu option, android already provides the widget to use it seamlessly.You can also handle a click listener to integrate the search mechanism into the app with the help of API's.
I hope it helps.

onCreateOptionsMenu in fragment not showing for API <= 16

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.

Appcompat three dot menu not displayed on Android < 3

I would like to display three dot menu in my app also on devices with android lower than version 3.0.
But on devices with Android version < 3 is menu displayed only after menu button pressing.
I tried to find any wotking solution for this but none from them working from me.
What is most easy way how to solve it on all devices?
Im using appCompat_v7_3
Many thanks for any advice.
Here is code of menu and activity.
menu main_activity.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/last_position_menu_btn"
android:icon="#drawable/ic_drawer"
android:title="#string/last_positions"
android:showAsAction="always"/>
<item android:id="#+id/settings_menu_btn"
android:icon="#drawable/ic_drawer"
android:title="#string/app_settings"
android:showAsAction="always"/>
</menu>
onCreateOptionMenu from main Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return super.onCreateOptionsMenu(menu);
}
Your 3.0 device has a hardware menu button. Android does not display the overflow icon when there is a hardware menu button present.
See this question or the documentation.

Categories

Resources