Saturday 4 January 2020

Context menu in Android with example

First of all we need to register view for using Context menu. This method will set the View.OnCreateContextMenuListener on the view to this activity, so onCreateContextMenu(ContextMenu, View, ContextMenuInfo) will be called when it is time to show the context menu.

public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
     menu.setHeaderTitle("Context Menu");
     menu.add(0, v.getId(), 0, "Action 1");
     menu.add(0, v.getId(), 0, "Action 2");
     menu.add(0, v.getId(), 0, "Action 3");
}

onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
                This method is triggered when the screen is long pressed and context menu view is being built
menu –  The actual context menu being built, using which we can set its properties.
                                                     Download Code

public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Context Menu");
    menu.add(0, v.getId(), 0, "Action 1");
    menu.add(0, v.getId(), 0, "Action 2");
    menu.add(0, v.getId(), 0, "Action 3");
}

onContextItemSelected(MenuItem item)
                This is called whenever an item in the context menu is selected and the interface MenuItem provides necessary details on the selected item. Through which we can write our own functionality to be perform based on item selection. 


public boolean onContextItemSelected(MenuItem item) {
   if (item.getTitle() == "Action 1") {
      Toast.makeText(this, "Action 1 invoked", Toast.LENGTH_SHORT).show();
   }
   else if (item.getTitle() == "Action 2") {
      oast.makeText(this, "Action 2 invoked", Toast.LENGTH_SHORT).show();
   }
   else if (item.getTitle() == "Action 3") {
      Toast.makeText(this, "Action 3 invoked", Toast.LENGTH_SHORT).show();
   } 
   else {
      return false;
   }
   return true;
}

On the layout page I have just display a text and button, long press button to see the output shown below.
Context Menu
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to compiletimeerror.com" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Long press me!" />

</LinearLayout>

MainActivity.java


package com.example.contextmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  Button btn = (Button) findViewById(R.id.btn);
  registerForContextMenu(btn);
 }
 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  super.onCreateContextMenu(menu, v, menuInfo);
  menu.setHeaderTitle("Context Menu");
  menu.add(0, v.getId(), 0, "Action 1");
  menu.add(0, v.getId(), 0, "Action 2");
  menu.add(0, v.getId(), 0, "Action 3");
 }
 @Override
 public boolean onContextItemSelected(MenuItem item) {
  if (item.getTitle() == "Action 1") {
   Toast.makeText(this, "Action 1 invoked", Toast.LENGTH_SHORT).show();
  } else if (item.getTitle() == "Action 2") {
   Toast.makeText(this, "Action 2 invoked", Toast.LENGTH_SHORT).show();
  } else if (item.getTitle() == "Action 3") {
   Toast.makeText(this, "Action 3 invoked", Toast.LENGTH_SHORT).show();
  } else {
   return false;
  }
  return true;
 }

}
Download Source code of this application

No comments:

Donate