Create Context Menu from XML File 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>

Create menu folder in res folder. Create my_menu.xml file contains menus as below:

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

    <item
        android:id="@+id/menu1"
        android:title="@string/menu1"></item>
    <item
        android:id="@+id/menu2"
        android:title="@string/menu2" />
    <item android:title="@string/menu3" >
        <menu >
            <item
                android:id="@+id/menu3_1"
                android:title="@string/menu3_1" />
            <item
                android:id="@+id/menu3_2"
                android:title="@string/menu3_2" />
        </menu>
    </item>

</menu>

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.MenuInflater;
import android.view.MenuItem;
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 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) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.my_menu, contextMenu);
        super.onCreateContextMenu(contextMenu, view, contextMenuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu1) {
            Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
        } else if (item.getItemId() == R.id.menu2) {
            // 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() == R.id.menu3_1) {
            Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
        } else if (item.getItemId() == R.id.menu3_2) {
            Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
        }
        return super.onContextItemSelected(item);
    }

}