We usually in the development process, in order to be flexible, in addition to the use of static menu, but also the need to add dynamic menu. Today’s features are as follows:

  1. In the upper right corner of the screen, there are more options. When clicked, there are two submenus: About and Exit

  2. Click on “About” and a dialog box pops up with a sentence

  3. Click “Exit”, a dialog box pops up, and the user clicks “OK” to close the entire page; Click “Cancel”, do not close the page

The implementation idea is as follows:

  1. Clone onCreateOptionsMenu method, call Menu add method in this method, dynamically add Menu, and set Menu order and content

  2. Duplicates the onOptionsItemSelected method in which menu click events are handled

  3. Two separate methods are provided for the display of the About dialog box and the Exit dialog box respectively

The source code is as follows:

1, the main Activity

import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import androidx.annotation.NonNull; import com.example.memorydemo.R; public class SimpleMenu extends Activity { private static final String TAG = "SimpleMenu"; @Override protected void onCreate(Bundle onSavedInstance) { super.onCreate(onSavedInstance); setContentView(R.layout.simple_menu); } @override public Boolean onCreateOptionsMenu(Menu Menu) {// Add (0, 0, 0, "About"); Add (0, 1, 1, "Exit"); Log.i(TAG, "call onCreateOptionsMenu"); return true; } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { super.onOptionsItemSelected(item); Switch (item.getitemid ()) {case 0: showDialog(); break; case 1: showExitDialog(); break; default: } return true; } private void showDialog() { new AlertDialog.Builder(this) .setTitle("About") .setMessage("This is just a demo.") .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); } private void showExitDialog() { new AlertDialog.Builder(this) .setTitle("Exit") .setMessage("Are you sure to EXIT?" ) .setPositiveButton("Sure", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); }}Copy the code

2, simple layout file simple_menu. XML, put TextView in the middle of the screen:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
 
    <TextView
            android:text="This is a simple menu demo."
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_height="wrap_content" android:id="@+id/textView5" android:layout_weight="1"/>
</LinearLayout>
Copy the code

3. The effect picture is as follows:

Here’s a “pit” to watch out for:

If the Activity or the entire application to use the parent Theme “. Theme AppCompat. Light. DarkActionBar “Theme, such as:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Copy the code

Menu not displayed!!

Welcome to exchange ~