Activity Menu : Menu « UI « Android






Activity Menu

  



package app.test;

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


public class Test extends Activity {
  public static final int ITEM0 = Menu.FIRST;
  public static final int ITEM1 = Menu.FIRST + 1;

  Button button1;
  Button button2;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button1.setVisibility(View.INVISIBLE);
    button2.setVisibility(View.INVISIBLE);
    

    }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, ITEM0, 0, "button1");
    menu.add(0, ITEM1, 0, "button2");
    menu.findItem(ITEM1);
    return true;
  }
  
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case ITEM0: 
      actionClickMenuItem1();
    break;
    case ITEM1: 
      actionClickMenuItem2(); break;

    }
    return super.onOptionsItemSelected(item);}
  private void actionClickMenuItem1(){
    setTitle("button1");
    button1.setVisibility(View.VISIBLE);
    button2.setVisibility(View.INVISIBLE);
  }

  private void actionClickMenuItem2(){
    setTitle("button2");
    button1.setVisibility(View.INVISIBLE);
    button2.setVisibility(View.VISIBLE);
  }
}

//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="@string/hello" />
  <Button android:id="@+id/button1"
    android:layout_width="100px"
    android:layout_height="wrap_content" android:text="@string/button1" />
  <Button android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="@string/button2" />
</LinearLayout>


//strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">Hello World, ActivityMenu</string>
  <string name="app_name">HelloMenu</string>
  <string name="button1">button1</string>
  <string name="button2">button2</string>
</resources>

   
    
  








Related examples in the same category

1.Create Option menu
2.Menu and messages
3.Context menu
4.Custom menu
5.Using Icon in Menu
6.Option Menu selection event
7.Menu Inflation
8.Context Menu event
9.Create Menu within your code
10.Define menu in xml file
11.Adding submenu
12.Using Icon in menu and submenu
13.Get Menu title
14.onOptionsItemSelected/onPrepareOptionsMenu/onCreateOptionsMenu Event
15.Basics of the Action Bar and how it interoperates with the standard options menu.
16.Demonstration of displaying a context menu from a fragment.
17.Demonstrates how fragments can participate in the options menu.
18.Demonstrates inflating menus from XML.
19.Usage of SearchView in an ActionBar as a menu item.
20.This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL