All About Android-Sample Code - Creating Option Menu in Android-ANDROIDKAWE

All About Android-Sample Code - Creating Option Menu in Android-ANDROIDKAWE - Welcome to ANDROID KAWE, Ini adalah sebuah blog directory tentang burung,baik itu suara burung ataupun tips perawatan burung.Blog ini adalah situs perangkum otomatis dari pencarian google yang kali ini berjudul All About Android-Sample Code - Creating Option Menu in Android-ANDROIDKAWE.Ada ribuan artikel yang sudah terekam ke dalam situs ini,silahkan cari sesuai yang kalian kehendaki,untuk artikel selanjutnya tentang judul diatas bisa kalian baca di bawah ini.

Konten : All About Android-Sample Code - Creating Option Menu in Android-ANDROIDKAWE
Judul : All About Android-Sample Code - Creating Option Menu in Android-ANDROIDKAWE

lihat juga


All About Android-Sample Code - Creating Option Menu in Android-ANDROIDKAWE


Sample Code - Create a Option Menu for Android Application

The option menu is a core menu of  the application which provides the basic navigation for applications.

I have written this post the explain the use of option menu in android application.


The best way to create an option menu is to define the option menu in a XML file and inflate the menu resource in onCreateOptionMenu method.

Create a project and define an activity as CreativeMenus (you can use any other name for your activity).


Create the XML file in res->layout directory as menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action1"
android:icon="@drawable/ic_launcher"
android:title="@string/action1"
/>
<item android:id="@+id/action2"
android:icon="@drawable/ic_launcher"
android:title="@string/action2" />
<item android:id="@+id/action3"
android:icon="@drawable/ic_launcher"
android:title="@string/action3" />
<item android:id="@+id/action4"
android:icon="@drawable/ic_launcher"
android:title="@string/action4" />
</menu>

Now implement the activity class as follows

package com.example.creativeapps;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class CreativeMenus extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action1: //start another activity
startActivity(new Intent(this, CreativelayoutsActivity.class));
return true;
case R.id.action2:
//TODO: handle the code for action 2
return true;
case R.id.action3:
//TODO: handle the code for action 3
return true;
case R.id.action4:
//TODO: handle the code for action 4
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}


To handle the click events on action items you need to implement the onOptionsItemSelected method of your activity class. Get the unique id defined for each item to handle the proper action for click on menu item.


See in the above example we are starting the another activity on click of action1. you can extend the example by implementing your own actions.


Menus are good to implement for version lower then 3.0 for Android version 3.0 or greater implement the action bar.


Please provide your feedback and share this to your creative android friends.


Creative Android Apps






Demikianlah Artikel All About Android-Sample Code - Creating Option Menu in Android-ANDROIDKAWE

Semoga artikel tentang All About Android-Sample Code - Creating Option Menu in Android-ANDROIDKAWE, mudah-mudahan bisa memberi manfaat untuk anda semua.

Anda sedang membaca artikel All About Android-Sample Code - Creating Option Menu in Android-ANDROIDKAWE dan artikel ini url permalinknya adalah https://androidkawe.blogspot.com/2012/07/all-about-android-sample-code-creating.html Semoga artikel ini bisa bermanfaat.Sekali lagi,ini adalah situs auto yang tidak ditulis langsung oleh admin,Kami tidak menjamin akan kebenaran dari artikel yang tertulis.
Back To Top