ContextMenu

ContextMenu

/res/layout/main.xml file

<?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" />
	<Chronometer android:id="@+id/ChronoMeter01"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:format="Timer: %s" />
</LinearLayout>

menu file

/res/menu/menus1.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:id="@+id/start_timer" android:title="Start" />
	<item android:id="@+id/stop_timer" android:title="Stop" />
	<item android:id="@+id/reset_timer" android:title="Reset" />
</menu>

/res/menu/menus2.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:id="@+id/item01" android:title="item01" />
	<item android:id="@+id/item02" android:title="item02" />
</menu>

Activity file

package com.androidwork.contextmenudemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Chronometer;
import android.widget.TextView;

public class ContextMenuDemoActivity extends Activity {
	/** Called when the activity is first created. */
	Chronometer cm;
	Menu menu;
	TextView tv;

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

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.TextView01) {
			getMenuInflater().inflate(R.menu.menus2, menu);

		}

		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		if (v.getId() == R.id.ChronoMeter01) {
			// getMenuInflater().inflate(R.menu.timer_context, menu);
			getMenuInflater().inflate(R.menu.menus1, menu);
			menu.setHeaderIcon(android.R.drawable.ic_media_play)
					.setHeaderTitle("Timer controls");
		}
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onContextItemSelected(item);
		boolean result = false;
		Chronometer timer = (Chronometer) findViewById(R.id.ChronoMeter01);
		switch (item.getItemId()) {
		case R.id.stop_timer:
			timer.stop();
			result = true;
			break;
		case R.id.start_timer:
			timer.start();
			result = true;
			break;
		case R.id.reset_timer:
			timer.setBase(SystemClock.elapsedRealtime());
			result = true;
			break;
		}
		return result;
		// return super.onContextItemSelected(item);
	}

}

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