Adding menu options to your android project

From D3xt3r01.tk
Jump to navigationJump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

HOW

A simple menu with a toast pop-up.

package com.learn;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class learn extends Activity {
	
    private static final int MENU_NEW_GAME = Menu.FIRST;
    private static final int MENU_QUIT = Menu.FIRST + 1;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_NEW_GAME, 0, "New Game");
        menu.add(0, MENU_QUIT, 0, "Quit");
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_NEW_GAME:
        	Toast.makeText(learn.this, "You clicked new game !", Toast.LENGTH_SHORT).show();
            return true;
        case MENU_QUIT:
            this.finish();
            return true;
        }
        return false;
    }
    
}

LINKS

Android Menus Guide