onCreateOptionsMenu is not doing anything for me - android

I'm totally stuck why this is happening. The options menu was working just fine, but now it's no longer working.
When I hit the menu button, the menu opens, I tap it and nothing. It does register the MenuItem when I do my LogCat... I'm seriously going to pull my hair, I don't understand why this is happening.
06-06 22:19:07.899:
DEBUG/MYTAG(23124): Stupid clicker
id=save settings item id=2133000192
id2=2133065728
Below is the code and xml
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings, menu);
Log.d("MYTAG", "Clicked saved1");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
Log.d("MYTAG", "Stupid clicker id="+item+" item id="+item.getItemId()+" id2="+R.mainmenuSettings.save1);
switch(item.getItemId()){
case R.mainmenuSettings.save1:
Log.d("MYTAG", "Stupid clicker");
break;
}
return super.onOptionsItemSelected(item);
}
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+mainmenuSettings/save1"
android:title="save settings"/>
</menu>

There's an error in your menu XML. The "id" attribute needs to start with "#+id/", so this needs to be "#+id/mainmenuSettingsSave1" or something similar. Also, you'll need to reference this in your code as R.id.mainmenuSettingsSave1:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mainmenuSettingsSave1"
android:title="save settings"/>
</menu>
and
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings, menu);
Log.d("MYTAG", "Clicked saved1");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
Log.d("MYTAG", "Stupid clicker id="+item+" item id="+item.getItemId()+" id2="+R.id.mainmenuSettingsSave1);
switch(item.getItemId()){
case R.id.mainmenuSettingsSave1:
Log.d("MYTAG", "Stupid clicker");
break;
}
return super.onOptionsItemSelected(item);
}

Related

Menu items are not showing on my android phone

I am running this code on my android phone and i am unable to see menu items on my android phone but when i run this on emulator, i can see menu items.
Please help . Here is the screenShot of.
<item android:title="About Us"
android:id="#+id/aboutUs_ID"/>
<item android:title="Preferences"
android:id="#+id/preferences_ID"/>
java code in activity
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater myMenu = getMenuInflater();
myMenu.inflate(R.menu.menu_file, menu);
return true;
}
Try to use below solution;
res/menu/main.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/action_settings"
android:orderInCategory="100"
android:title="Color Theam"
app:showAsAction="never" />
<item
android:id="#+id/action_appMahiti"
android:orderInCategory="100"
android:title="#string/tab_refrence"
app:showAsAction="never" />
</menu>
Here is the code snippiest you can add in your activity class
Activity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
// perform operation or display message
Toast.makeText(this, "CLick me", Toast.LENGTH_SHORT).show();
break;
case R.id.action_appMahiti:
// perform operation
startActivity(new Intent(this, XYZ.class));
break;
}
return super.onOptionsItemSelected(item);
}
I will suggest to try to use above solution.

Action items not selectable Android Sherlock Actionbar

I'm implementing action bar with items. I can't see the selected item effect (background selection color) when I click on an item in the actions :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_item_share_action_provider_action_bar"
android:showAsAction="always"
android:title="TTTTTTT"
android:checkable="true"
/>
<item android:id="#+id/menu_item_share_action_provider_action_bar"
android:showAsAction="always"
android:title="AAAAAA"
android:checkable="true"
/>
</menu>
On My SherlockActivity :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
UPDATE:
You need to do something like this (switching the icons ids):
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
-----------
Have you done something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
This is needed to inflate the XML file.
Maybe setting identical id's on both items is causing the problem. Did you try to change one of them?

Option Menu does not appear in Android

I have this code to create the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.tip_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MNU_PREV:
animateTextViewsPrev();
break;
case MNU_NEXT:
animateTextViewsNext();
break;
}
return true;
}
And the XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/prev_tip" android:title="#string/prevTip"></item>
<item android:id="#+id/next_tip" android:title="#string/nextTip"></item>
</menu>
In a smartphone with Android 2.1 the menu is visible but in other mobile whit 4.1.1 is invisible.
Somebody now how to solve it?
What is you target Android, good to know, in android 4.0 them has redesign the menu layout.
I think you is missing super.onCreateOptionsMenu(menu); in the call onCreateOptionsMenu
In my code I has,
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
I was dealing with the same problem.. read some queries and documentation.. Hope this might help you.
Here's my XML file for a menu..
<item
android:id="#+id/action_send_feedback"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_send_feedback"/>
<item
android:id="#+id/action_share_app"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="#string/action_share_app"
android:icon="#drawable/ic_action_share" />
<item
android:id="#+id/action_rate_app"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_rate_app"/>
JAVA Code goes here..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
For android phones which have option button (at the bottom of the phone) the menu item which are showAsAction="never" comes when the button is pressed.. or else they will be shown normally on the action bar options menu..
Ref: http://developer.android.com/guide/topics/ui/menus.html#options-menu
You can simply change the "targetSdkVersion" to 10 in manifest file
It needs the ID in the java! :)

Android Hardware Menu button isn't working for me

I'm using this code to display a menu, but it isn't doing anything when I press the menu button.
This is in a view flipper, I don't know if that has anything to do with the problem.
I used this before and with no problems at all:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.new_game:
return true;
case R.id.help:
finish();
return true;
}
return false;
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:title="new_game" />
<item android:id="#+id/help"
android:title="clear" />
</menu>
You are not calling the superclass methods.
Dont you have to have some icon attached to it?
android:icon="#drawable/ic_menu_add"
for example

Android menu will not appear when the menu button is pressed in AVD

I have defined a menu as laid out by http://developer.android.com/guide/topics/ui/menus.html
Here is my menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_home"
android:icon="#drawable/ic_menu_home"
android:title="Main Menu" />
<item android:id="#+id/menu_signout"
android:icon="#drawable/ic_menu_signout"
android:title="Sign Out" />
</menu>
and my code, placed in my activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_home:
startActivity(new Intent(ManageUsersActivity.this, MainMenuActivity.class));
finish();
return true;
case R.id.menu_signout:
//TODO: issue signout to clear the cookie
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Unfortunately, when I click the menu button, nothing happens.
Try chaining to the superclass in onCreateOptionsMenu(). Here is a sample project demonstrating the use of MenuInflater for options and context menus.

Categories

Resources