android context menu example

      Comments Off on android context menu example

android context menu example

android context menu example

android context menu example is explained using android chronometer. Here in this android tutorial i have used android chronometer to display timer and want to stop start or reset timer using Context Menu. for displaying context menu you need to add separate XML file of the menu later on you need to inflate this menu file with to display menu at run time. Check complete code of on how to display context menu in android or how to use chronometer in android.

Questions related with Context Menu and Chronometer in Android

  • Create a context menu for the Chronometer control. Also write line of code to
    explain how to handle the ContextMenu clicks
  • What is Context Menu? How it is different from Option Menu? Explain
    creating context menu taking suitable example
  • When I click the menu button on the emulator, is that option menu? YES
  • By long-tapping on any widget will display Context Menu : YES
  • Write a code example that creates context menu for the Chronometer control.
    Also explain how to handle the ContextMenu clicks in the Activity
  • Explain with code snippet creation of context menu for Chronometer control
    and how to handle the ContextMenu clicks?
  • Explain context menu with example
  • Explain onCreateContextMenu() and registerForContextMenu()
    methods.
  • Differentiate Context Menu and Option Menu
  • or what purpose Chronometer is used? Explain the Chronometer object’s format attribute and explain the different methods associated with Chronometer
  • What is uses of Chronometer widgets and list its important properties and event
  • Which methods can be used with Chronometer object?

Location : res/location

File : activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4527a0"
    tools:context="android.context.menu.MainActivity" >

    <Chronometer
        android:id="@+id/chronometer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="25dp"
        android:textColor="#d1c4e9"
        android:text="Chronometer" />

    <TextView
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="64dp"
        android:text="By Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#d1c4e9" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Android Example on"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#d1c4e9" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_toRightOf="@+id/chronometer1"
        android:text="Chronometer"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#d1c4e9" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="21dp"
        android:layout_toLeftOf="@+id/chronometer1"
        android:text="ContextMenu"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#d1c4e9" />

</RelativeLayout>

Location : src/package

File : MainActivity.java

package android.context.menu;

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;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Chronometer c = (Chronometer) findViewById(R.id.chronometer1);
		
		registerForContextMenu(c);
	}

	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		
		if(v.getId()==R.id.chronometer1)
		{
			getMenuInflater().inflate(R.menu.startstopmenu, menu);
			menu.setHeaderIcon(android.R.drawable.ic_media_play);
			menu.setHeaderTitle("Timer Control");
		}
			
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		//return super.onContextItemSelected(item);
		
		if(item.getTitle().toString().equals("Start"))
		{
			Chronometer c = (Chronometer) findViewById(R.id.chronometer1);
			c.start();
		}
		else if(item.getTitle().toString().equals("Stop"))
		{
			Chronometer c = (Chronometer) findViewById(R.id.chronometer1);
			c.stop();
		}
		else if(item.getTitle().toString().equals("Reset"))
		{
			Chronometer c = (Chronometer) findViewById(R.id.chronometer1);
			c.setBase(SystemClock.elapsedRealtime());
			c.start();
		}
		return false;
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		
		
		return super.onOptionsItemSelected(item);
	}
}

 

Location: res\menu

File : startstopmenu.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>

 

Output 1

android context menu example

Output 2

android context menu example

Output 3

android context menu example