Android Options menu not showing

I’m making a game for Android, and the simplest implementation of the settings menu doesn’t work - the game itself is in a separate class (Engine.java) that extends LinearLayout, so I wonder if this is why it is not visible - however the other “top level” @Overrides, like onStop (), onPause (), etc., all the lights are just perfect - is there something I have to do in my game class so that it displays the options menu

The code base from the example Pro Android Games SpaceBlaster: http://www.apress.com/9781430226475 (source can be downloaded)

MyGame.class (application entry point):

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LayoutInflater factory = LayoutInflater.from(this); // Set game layout view = factory.inflate(R.layout.main, null); setContentView(view); // Enable view key events view.setFocusable(true); view.setFocusableInTouchMode(true); } //End OnCreate() @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } 

Res \ menu \ menu.xml:

 <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/icon" android:icon="@drawable/icon" /> <item android:id="@+id/text" android:title="Text" /> <item android:id="@+id/icontext" android:title="Icon and text" android:icon="@drawable/icon" /> </menu> 

layout \ main.xml:

 <game.thegame.test.Engine xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_absolute" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FF000000"> </game.thegame.test.Engine> 
+4
source share
3 answers

Make sure that all menu items have a title !

Elements without a title, I think, can appear on the action bar, but not as a list when you click a menu button. The following is a simple example.

 <menu xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_menu" > <item android:id="@+id/action_undo" android:icon="@drawable/ic_action_undo" android:title="@string/mb_undo" android:showAsAction="always" android:onClick="onPressUndo" /> 
+1
source

As far as I can tell, if everything is fine with the code and menu resource files, the reason is not to add an additional style resource directory for the low-level API.

Here is my set:

AndroidManifest.xml File

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.levent.learning.notetaker"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

A) MainActivity.java

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } } 

B) The resource file for the menu

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/hede" android:title="@string/hede_item"></item> </menu> 

C) Resource File: styles.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppBaseTheme" parent="android:Theme.Light"> </style> <style name="AppTheme" parent="AppBaseTheme"> </style> </resources> 

Everything seems perfect with these rights? But even so, the Options menu is not displayed. Thus, I created and added a resource directory for API level 11, named value-v11, and added an additional styles.xml file for it.

SOLUTION 1

D) Resource File for API Level 11

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppBaseTheme" parent="android:Theme.Holo.Light"> </style> </resources> 

Then it will work!

Here is my working configuration, to make two different folders for the resource of values, I selected the Project option in Android;

enter image description here

SOLUTION 2

Remove the following line of the theme definition from the AndroidManifest.xml file;

 android:theme="@style/AppTheme" 

So, the new AndroidManifest.xml looks like this:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.levent.learning.notetaker"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

I am new to Android development using Android Studio version 2.1.2 . Even this question seems so old, it is not out of date. I hope this answer helps someone because this problem drives me crazy until I find an empirical solution, hope it helps. Happy coding!

+1
source

Directly we can add menu items like this, it works fine for me

  @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub super.onCreateOptionsMenu(menu); MenuItem item1=menu.add(0, 4, 0,"text1"); item1.setIcon(R.drawable.car); MenuItem item2=menu.add(0, 0, 0, "text2"); item2.setIcon(R.drawable.share); MenuItem item3=menu.add(0, 2, 0, "text3"); item3.setIcon(R.drawable.history); MenuItem item4=menu.add(0, 3, 0, "text4"); item4.setIcon(R.drawable.settings); return true; } 
0
source

Source: https://habr.com/ru/post/1369589/


All Articles