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