Android Hardware Menu button isn't working for me - android

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

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.

Android - Cannot see Options Menu when extends SherlockFragmentActivty

![Layout Look like this][1]
[1]strong text: http://i.stack.imgur.com/CIsz8.png
I am trying to create a menu, but I am unable to see the menu.i am able to see menu in fragments properly. but cannot see in the sherlockfragmentactivity that is my main frame for fragment.
when i click on menu they doesn't take any action.
menu code
<group android:id="#+id/dashbardmenu">
<item
android:id="#+id/abc"
android:title="ABC">
</item>
<item
android:id="#+id/setting"
android:title="Setting">
</item>
</group>
<group android:id="#+id/fragmentmenu" >
<item
android:id="#+id/form"
android:icon="#drawable/ic_send"
android:showAsAction="ifRoom"
android:title="Form">
</item>
<item
android:id="#+id/resetform"
android:title="Reset Form">
</item>
</group>
im using visibility & invisibility for particular file.
java code
1st way
public boolean onCreateOptionaMenu(Menu menu ){
MenuInflater inflater = this.getSupportMenuInflater();
inflater.inflate(R.menu.activity, menu);
menu.setGroupVisible(R.id.fragmentmenu, false);
return super.onCreateOptionsMenu(menu);
}
2nd way
public boolean onCreateOptionaMenu(Menu menu,MenuInflater inflater ){
inflater.inflate(R.menu.activity, menu);
menu.setGroupVisible(R.id.fragmentmenu, false);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected((MenuItem)item);
switch (item.getItemId()){
case R.id.abc:
//Some action
case R.id.setting:
//Some action
default:
return false;
}
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected((MenuItem)item);
switch (item.getItemId()){
case R.id.abc:
//Some action
case R.id.setting:
//Some action
default:
return true;
}
}
Where is my Mistake Anyone help me???
Did you call this?
setHasOptionsMenu(true);
Also you have a typo - method name is onCreateOptionsMenu but you have twice onCreateOptionaMenu

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! :)

onCreateOptionsMenu is not doing anything for me

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

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