Create Project
Create new project in Android Studio with steps as below:
Step 1: Input Project Name and Select Project Location
Step 2: Select SDK for Android App
Step 3: Select Default Activity for App
Step 4: Finish create project
Add Strings
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 XML
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>
Main Activity Layout
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>
Main Activity Class
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);
}
}
Structure of Project
Run App
Load Main Activity
Open Context Menu
Open Menu Item Have Children in Context Menu
Selected Menu in Context Menu
Get Item Info is selected from Context Menu