Create Dynamically Context Menu in Android


Create new project in Android Studio with steps as below:




Open res\values\strings.xml file and add new string as below:

<resources>
    <string name="app_name">Learn Android with Real Apps</string>
    <string name="menu1">Menu 1</string>
    <string name="menu2">Menu 2</string>
    <string name="menu3">Menu 3</string>
    <string name="menu3_1">Menu 3.1</string>
    <string name="menu3_2">Menu 3.2</string>
</resources>

Open res\layout\activity_main.xml file and create layout as below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listViewProduct"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Add code to MainActivity.java in android.demo.learnandroidwithrealapps package as below:

package android.demo.learnandroidwithrealapps;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final int MENU1 = 0;
    private static final int MENU2 = 1;
    private static final int MENU3 = 2;

    private static final int MENU_1_ITEM = Menu.FIRST;
    private static final int MENU_2_ITEM = Menu.FIRST + 1;
    private static final int MENU_3_ITEM = Menu.FIRST + 2;
    private static final int MENU_3_1_ITEM = MENU_3_ITEM + 1;
    private static final int MENU_3_2_ITEM = MENU_3_ITEM + 2;

    private ListView listViewProduct;
    private List<String> products;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        loadData();
    }

    private void initView() {
        listViewProduct = findViewById(R.id.listViewProduct);
        registerForContextMenu(listViewProduct);
    }

    private void loadData() {
        products = new ArrayList<String>();
        products.add("Name 1");
        products.add("Name 2");
        products.add("Name 3");
        products.add("Name 4");
        products.add("Name 5");
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, products);
        listViewProduct.setAdapter(arrayAdapter);
    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
        contextMenu.add(MENU1, MENU_1_ITEM, 0, getText(R.string.menu1));
        contextMenu.add(MENU2, MENU_2_ITEM, 1, getText(R.string.menu2));
        SubMenu menu3 = contextMenu.addSubMenu(MENU3, MENU_3_ITEM, 2, getText(R.string.menu3));
        menu3.add(MENU3, MENU_3_1_ITEM, 0, getText(R.string.menu3_1));
        menu3.add(MENU3, MENU_3_2_ITEM, 1, getText(R.string.menu3_2));
        super.onCreateContextMenu(contextMenu, view, contextMenuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getItemId() == MENU_1_ITEM) {
            Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
        } else if (item.getItemId() == MENU_2_ITEM) {
            // Get Product Name is selected from context menu
            AdapterView.AdapterContextMenuInfo adapterContextMenuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
            int position = adapterContextMenuInfo.position;
            String product = products.get(position);
            Toast.makeText(getApplicationContext(), product, Toast.LENGTH_SHORT).show();
        } else if (item.getItemId() == MENU_3_1_ITEM) {
            Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
        } else if (item.getItemId() == MENU_3_2_ITEM) {
            Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
        }
        return super.onContextItemSelected(item);
    }

}