I am new to programming. I try to create a simple menu in android studio, but when I try to create it, it does not appear in the emulator, I don’t know what the problem is. I do everything exactly as described in the instructions.

code main.xml:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:showAsAction="never" /> <item android:id="@+id/menu_other" android:title="@string/test_other" android:onClick="onClickMenuOption"/> <item android:id="@+id/menu_exit" android:title="Exit" android:onClick="onClickMenuExit"/> </menu> 

code MainActivity.java:

 package com.examples.nick.testmenu; import android.app.Activity; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; 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) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void onClickMenuOption(MenuItem item) { Toast toast = Toast.makeText(this, "You Clicked on Other", Toast.LENGTH_LONG); toast.show(); } public void onClickMenuExit(MenuItem item) { finish(); } } 

When I start the emulator, I get a white window with the inscription "HelloWorld!" without any menu. ((

    2 answers 2

    If it’s quite simple, you can override the onCreateOptionsMenu method to create and the onOptionsItemSelected method to handle menu buttons:

     public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add("menu1"); menu.add("menu2"); menu.add("menu3"); menu.add("menu4"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show(); return super.onOptionsItemSelected(item); } } 
    • Thanks for the answer, the solution turned out to be simpler: Overflow menu is hidden in NavigationBar in phones that have a "Menu" button, and is activated accordingly when you press the "Menu" button, this is relevant for version up to 4.4.2 Kit-Kat. For older versions, there is an Action Bar with Action overflow. - Nikolai
     @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.menu_other) { return true; } if (id == R.id.menu_exit) { return true; } return super.onOptionsItemSelected(item); } 

    And instead of inheriting from an Activity it is better to use AppCompatActivity in it with a toolbar and a plus to using materialdesign