Menu

Menu

/res/layout/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:id="@+id/TextView01" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>

Activity file

package com.androidwork.menudemo1;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.TextView;

public class MenuDemo1Activity extends Activity // implements
												// OnMenuItemClickListener {
{
	/** Called when the activity is first created. */
	private int searchBtnId = Menu.FIRST;
	private int scheduleBtnId = Menu.FIRST + 1;
	private int playBtnId = Menu.FIRST + 2;
	private int stopBtnId = Menu.FIRST + 3;

	private int group1Id = 1;
	private int group2Id = 2;

	Menu menu;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TextView tv = (TextView) findViewById(R.id.TextView01);
		registerForContextMenu(tv);

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		menu.setHeaderTitle("Context Menu");
		menu.add("item");
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		TextView tv = (TextView) findViewById(R.id.TextView01);
		tv.setText(item.getTitle());
		return super.onContextItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(group1Id, searchBtnId, searchBtnId, "Search").setIcon(
				android.R.drawable.ic_menu_search);

		menu.add(group2Id, scheduleBtnId, scheduleBtnId, "Schedule")
				.setIcon(android.R.drawable.ic_lock_idle_alarm)
				.setAlphabeticShortcut('0');
		menu.add(group2Id, playBtnId, playBtnId, "Play").setIcon(
				android.R.drawable.ic_media_play);
		menu.add(group2Id, stopBtnId, stopBtnId, "Stop").setIcon(
				android.R.drawable.ic_media_previous);
		// menu.setGroupVisible(1, false);
		return super.onCreateOptionsMenu(menu);
		/* Dialer application */
		// menu.add("dialItem").setIcon(R.drawable.icon).setIntent(new
		// Intent(Intent.ACTION_DIAL));
		// return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		TextView tv = (TextView) findViewById(R.id.TextView01);
		tv.setText("you have selected : " + item.getTitle());

		switch (item.getItemId()) {
		case 1:
			tv.setBackgroundColor(Color.RED);
			break;
		case 2:
			tv.setBackgroundColor(Color.YELLOW);
			break;
		case 3:
			tv.setBackgroundColor(Color.GRAY);
			break;
		case 4:
			tv.setBackgroundColor(Color.BLUE);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}

Output

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s