English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Android Programming Basics: Menu Function Menu Design Example

This article instance explains the Menu feature menu in Android programming. Shared for everyone's reference, as follows:

The design of the Android functional menu, the program defines two menu sub-items, one is "About", and the other is "Exit". When clicking on "About", a new Toast prompt will be displayed, and when clicking on "Exit", the program will be terminated.

In addition to the default overridden onCreate, we also need to create two new class functions: onCreateOptionsmenu() and onOptionsItemSelected(). The former is used to create the Menu items, and the latter is for handling the event after the menu is selected and executed.

Let's take a look at the effect diagram:

We only made changes to one file, that is MenuDemo.Java, the code is as follows:

package com.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MenuDemo extends Activity {
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }
 // Create menu
 public boolean onCreateOptionsMenu(Menu menu) {
  menu.add(0, 0, 0, "About");
  menu.add(0, 1, 1exit());
  return super.onCreateOptionsMenu(menu);
 }
 //Menu Response
 public boolean onOptionsItemSelected(MenuItem item) {
  super.onOptionsItemSelected(item);
  switch (item.getItemId()) {
  case 0:
   Toast.makeText(MenuDemo.this, "Welcome to the Nahan Tutorial", Toast.LENGTH_LONG).show();
  case 1:
   this.finish();
  }
  return true;
 }
}

Readers who are interested in more about Android-related content can check the special topics on this site: 'Android Development Tutorial for Beginners and Advanced', 'Summary of Android View View Techniques', 'Summary of Activity Operation Techniques in Android Programming', 'Summary of Techniques for Operating SQLite Database in Android', 'Summary of Techniques for Operating JSON Format Data in Android', 'Summary of Database Operation Techniques in Android', 'Summary of File Operation Techniques in Android', 'Summary of SD Card Operation Methods in Android Programming Development', 'Summary of Resource Operation Techniques in Android', and 'Summary of Android Control Usage'

I hope this article will be helpful to everyone in Android program design.

Declaration: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been edited by humans, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @ when sending email) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like