วันอังคารที่ 3 กรกฎาคม พ.ศ. 2555

Android Menu

-->

Android Option Menu, Context Menu

Building Menus
A developer can implement three types of menus in Android, and this recipe creates an
example of each:
  • Options menu—The main menu for an Activity that displays when the MENUkey is pressed. It contains an Icon menu and possibly an Expanded menu when the More menu item is selected.
  • Context Menu—A floating list of menu items that displays when a view is long pressed.
  • Submenu—A floating list of menu items that displays when a menu item is pressed.
The Options menu is created the first time the MENU key is pressed in an activity.This
launches the onCreateOptionsMenu() method that usually contains Menu methods,
such as:
menu.add(GROUP_DEFAULT, MENU_ADD, 0, "Add").setIcon(R.drawable.icon);

MenuExampleActivity.java Code

package android.example.menu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;

public class MenuExampleActivity extends Activity {
     
      // For Option MENU
      private final int GROUP=0;
      private final int MENU_1=1, MENU_2=2, MENU_3=3;
     
      // For Context MENU
      private final int ID_DEFAULT=0;
      private final int ID_TEXT1=1, ID_TEXT2=2, ID_TEXT3=3;
      private String[] choices = {"Press Me", "Try Again", "Change Me"};     
      private static int itemNum=0;
                       
      private static TextView bv;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        bv = (TextView) findViewById(R.id.focus_text);
        registerForContextMenu((View) findViewById(R.id.focus_text));
    }
   
    // Context Menu
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,   
            ContextMenuInfo menuInfo) {
      super.onCreateContextMenu(menu, v, menuInfo);
   
     
      if(v.getId() == R.id.focus_text) {
            SubMenu textMenu = menu.addSubMenu("Change Text");
            textMenu.add(0, ID_TEXT1, 0, choices[0]);
            textMenu.add(0, ID_TEXT2, 0, choices[1]);
            textMenu.add(0, ID_TEXT3, 0, choices[2]);
            menu.add(0, ID_DEFAULT, 0, "Original Text");
      }
    }
   
    @Override
    public boolean onContextItemSelected(MenuItem item) {
   
      switch(item.getItemId()) {
     
            case ID_DEFAULT:
                  bv.setText(R.string.hello);
                  return true;
            case ID_TEXT1:
            case ID_TEXT2:
            case ID_TEXT3:  
                  bv.setText(choices[item.getItemId()-1]);
                  return true;
      }
      return super.onContextItemSelected(item);
    }
   
   
 // OPTION MENU
    @Override
    public boolean onCreateOptionsMenu(Menu menu){   
      super.onCreateOptionsMenu(menu);
     
      menu.add(GROUP,MENU_1,0,"Menu1").setIcon(R.drawable.ic_launcher);
      menu.add(GROUP,MENU_2,0,"About");
      menu.add(GROUP,MENU_3,0,"Exit");   
      return super.onCreateOptionsMenu(menu);
    }
   
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
     
      switch (item.getItemId()) {
                  case MENU_1:
                        //startActivity(new Intent("com.app.menu"));
                        return true;     
                  case MENU_2:                                                     
                        startActivity(new Intent("com.code.menu.about"));
                        return true;           
                  case MENU_3:                                                     
                        System.exit(0);
                        return true;           
                  default:
                        break;
      }   
      return true;
    }
   
}//class

main.xml Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#570156"
    android:orientation="vertical" >

  <TextView
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Android Menu"
        android:textStyle="bold"
        android:textColor="#0000FF"
        android:textSize="30dp" />
   
    <TextView
        android:id="@+id/focus_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="15dp"
        android:background="#f4ed75"
        android:text="Context Menu Press"
        android:textColor="#FF0000"
        android:textSize="40dp" />

</LinearLayout>



 Download Android Menu Example Code




Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android

สอนเขียน Android  สอนเขียนโปรแกรม Android

ติดต่อ amphancm@gmail.com

-->