Context Menu In Android With Example

Hello friends, today I will explain you how to build a context Menu in Android with example. The context menu is also called Long Press Menu.

So the context menu is the one which is in the form of a list and it appears when the user hold a particular item within list or it can be applied on the button.

For Example: In Whatsapp, when you hold any particular contact within the list, a menu is opened with various options to select, that menu is called Context Menu.

Now we know what actually is context menu, we will now do the implementation of this.

Context Menu in Android

  1. Create a project in Android and name it as Context Menu.
  2. After creating the project, now create an xml file and name it as ids.xml and do following changes in it:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="delete" type="id">delete</item>
    <item name="update" type="id">update</item>
    <item name="select" type="id">select</item>
</resources>

3. Now go to the layout file content_main.xml and do following changes:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Context Menu"
        android:id="@+id/button"
        android:gravity="center"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
 
</RelativeLayout>

Now go to MainActivity.java file and do following changes:

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class ContextMenu extends ActionBarActivity implements View.OnClickListener{

    Button button;

    @Override
    public View onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        button = (Button)findViewById(R.id.button);
        registerForContextMenu(button);

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(Menu.NONE, R.id.delete, Menu.NONE, "Delete");
        menu.add(Menu.NONE, R.id.update, Menu.NONE, "Update");
        menu.add(Menu.NONE, R.id.select, Menu.NONE, "Select");

    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.delete:
                Toast.makeText(getActivity(),"Delete menu hit",Toast.LENGTH_SHORT).show();
               break;
            case R.id.update:
                Toast.makeText(getActivity(),"Update menu hit",Toast.LENGTH_SHORT).show();
                break;
            case R.id.select:
                Toast.makeText(getActivity(),"Select menu hit",Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

Now I will explain the above code one by one.

  • Firstly we have declared a button and its id is find in the onCreate method.
  • Now to create a context menu, we have to override two of its methods, these are: onCreateContextMenu  and onContextItemSelected.
  • In onCreateContextMenu, we add the list of items we want to add within the context menu, here we have taken three values for the list.
  • Next comes the onContextItemSelected, in this we will define what action will be preformed after the item within the context menu is selected.
  • Here I have just used a toast method, you can use intent to preform different activities.

So as we have created the context menu, now is the time to run it.

Output 1:

Context_menu1

 

So we have completed our application. Hope you enjoyed the tutorial.