Demonstration of ContextMenu in Android
In this tutorial, we will learn how to implement a ContextMenu in Android using XML and Java. A context menu appears when the user long-presses on a view (in this case, a TextView), providing additional options for interaction.
1. Create contextmenu_list.xml
inside res/menu
directory:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/setting"
android:title="Setting" />
<item
android:id="@+id/help"
android:title="Help" />
<item
android:id="@+id/about"
android:title="About Us" />
</menu>
2. Create activity_main.xml
layout file:
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#212121"
android:text="Long Press here..."
android:textColor="#FEFEFE"
android:textSize="35sp" />
</LinearLayout>
3. Create MainActivity.java
file:
package com.tecvipul.contextmenudemo;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
registerForContextMenu(textView); // Register context menu on TextView long press
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.contextmenu_list, menu);
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.setting:
Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show();
return true;
case R.id.help:
Toast.makeText(getApplicationContext(), "Help", Toast.LENGTH_SHORT).show();
return true;
case R.id.about:
Toast.makeText(getApplicationContext(), "About Us", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
}
4. Important Note (gradle.properties file)
If you face an error where resource IDs are not resolving in onContextItemSelected
method, simply add this line in your gradle.properties
file and sync your project:
android.nonFinalResIds = false
📜 Detailed Description:
In this example, we create a simple ContextMenu that appears when the user long-presses on a TextView
. The menu is defined in the contextmenu_list.xml
file with three options: Setting, Help, and About Us.
- activity_main.xml: Contains a
TextView
that the user will long press on to open the context menu. - MainActivity.java:
- We use
registerForContextMenu(textView)
to register the context menu on the TextView. - In
onCreateContextMenu()
, we inflate the XML menu resource. - In
onContextItemSelected()
, we handle the selected menu item and show a Toast message accordingly.
- We use
- gradle.properties: If you get an unresolved error for resource IDs, adding
android.nonFinalResIds = false
resolves the issue in newer Gradle setups.
This simple demonstration will help beginners understand how to integrate a context menu and handle user actions with ease.
0 Comments
If you have any doubts, Please let me know