Friday, 31 October 2014

Tutorial Membuat Option Menu di Andoid

Menu memiliki 3 jenis:

  1. Option Menu
  2. Context Menu
  3. 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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu 
  3.   xmlns:android="http://schemas.android.com/apk/res/android">
  4.     <item android:id="@+id/Color" android:title="Color">
  5.            <menu>
  6.             <item android:id="@+id/RedColor" android:title="Red"/>
  7.             <item android:id="@+id/GreenColor" android:title="Green"/>
  8.         </menu>
  9.    </item>
  10.    
  11. </menu>




Langkah 3: Edit "MenuOptionDemoActivity.java"

  1. package com.MenuOptionDemo;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Menu;
  5. import android.view.MenuInflater;
  6. import android.view.MenuItem;
  7. import android.widget.Toast;
  8. public class MenuOptionDemoActivity extends Activity {
  9.     /** Called when the activity is first created. */
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.     }
  15.     
  16.     @Override
  17.     public boolean onCreateOptionsMenu(Menu menu) {
  18.         // TODO Auto-generated method stub
  19.         MenuInflater inflater=getMenuInflater();
  20.         inflater.inflate(R.menu.optionmenu, menu);
  21.         
  22.         return super.onCreateOptionsMenu(menu);
  23.     }
  24.     
  25.     @Override
  26.     public boolean onOptionsItemSelected(MenuItem item) {
  27.         // TODO Auto-generated method stub
  28.         if(item.getItemId()==R.id.RedColor)
  29.         {
  30.             Toast.makeText(MenuOptionDemoActivity.this,"Red Color Selected" ,1000).show();
  31.         }
  32.         else if(item.getItemId()==R.id.GreenColor)
  33.         {
  34.             Toast.makeText(MenuOptionDemoActivity.this, "Green Color Selected", 1000).show();
  35.         }
  36.         return super.onOptionsItemSelected(item);
  37.     }
  38. }

Langkah 4: Mulai Emulator




Izzat Aziz


0 comments:

Post a Comment