Menu memiliki 3 jenis:
- Option Menu
- Context Menu
- Sub Menu
Langkah 1: Tambahkan "optionmenu.xml" Klik kanan pada "project" -> New -> Andorid Xml file
Langkah 2: Edit "optoinmenu.xml"
Project res -> menu -> optionmenu.xml
- <?xml version="1.0" encoding="utf-8"?>
- <menu
- xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:id="@+id/Color" android:title="Color">
- <menu>
- <item android:id="@+id/RedColor" android:title="Red"/>
- <item android:id="@+id/GreenColor" android:title="Green"/>
- </menu>
- </item>
- </menu>
Langkah 3: Edit "MenuOptionDemoActivity.java"
- package com.MenuOptionDemo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.widget.Toast;
- public class MenuOptionDemoActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // TODO Auto-generated method stub
- MenuInflater inflater=getMenuInflater();
- inflater.inflate(R.menu.optionmenu, menu);
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // TODO Auto-generated method stub
- if(item.getItemId()==R.id.RedColor)
- {
- Toast.makeText(MenuOptionDemoActivity.this,"Red Color Selected" ,1000).show();
- }
- else if(item.getItemId()==R.id.GreenColor)
- {
- Toast.makeText(MenuOptionDemoActivity.this, "Green Color Selected", 1000).show();
- }
- return super.onOptionsItemSelected(item);
- }
- }
Langkah 4: Mulai Emulator
0 comments:
Post a Comment