All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Contextual Menu Android

Last Updated : Mar 11, 2024

Contextual Menu Android

In this article we will show you the solution of contextual menu android, to describe a variety of options and activities in Android apps, there are three types of menus available.

The menu checklist in Android applications are as follows:

  • Android settings menu
  • Android popup menu
  • Android context menu

The context menu in Android is similar to a floating menu that appears when the user long presses or click-throughs such an item and is useful for having to implement features that describe the unique information or reference frame consequence.

The Android context menu is similar to the Windows or Linux right-click menu.

The context menu in the Android system does provide activities that start changing a specific component or context sequence in the user interface, and a context menu can be provided for any view.

Object shortcut keys and object icons will not be supported by the context menu.

The contextual response mode is an ActionMode system implementation that directs users to interact forward into performing contextual actions.

When a user activates this method by identifying a product, a contextual activity bar has seemed at the top of the screen, displaying actions that the user can take with the currently selected item (s).

Whenever the user permanently disables all products, needs to press the Button, or chooses the Done action on the left side of the bar, the intervention mode is disabled and the contextual intervention bar disappears.

A contextual menu provides actions that impact a specific UI item or context frame.

Context menus can be provided for just any view, but they are most commonly used only for products inside a ListView, GridView, or even other view collection where the user can take direct actions on each item.

Contextual actions can be provided in two ways:

In a context menu that is floating. Whenever the person uses a long-click (press and hold) on a perspective that declares support for a context menu, a menu will appear as a dangling list of menu options (similar to a dialogue).

Users can only perform one contextual action at a time. In the mode of contextual action.

This method is a standard edition of ActionMode that demonstrates at the top of the display a contextual action bar to action plans that actually impact the selected item (s).

Step By Step Guide On Contextual Menu Android :-

XML Code

<?xml version="1.0" encoding="utf-8"?>
<!--Relative Layout to display all the details-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Long press me!"
        android:textColor="#000"
        android:textSize="20sp"
        android:textStyle="bold" />
</RelativeLayout>

Java code

import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    TextView textView;
    RelativeLayout relativeLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
        relativeLayout = (RelativeLayout) findViewById(R.id.relLayout);
        registerForContextMenu(textView);
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Choose a color");
        menu.add(0, v.getId(), 0, "Yellow");
        menu.add(0, v.getId(), 0, "Gray");
        menu.add(0, v.getId(), 0, "Cyan");
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getTitle() == "Yellow") {
            relativeLayout.setBackgroundColor(Color.YELLOW);
        } else if (item.getTitle() == "Gray") {
            relativeLayout.setBackgroundColor(Color.GRAY);
        } else if (item.getTitle() == "Cyan") {
            relativeLayout.setBackgroundColor(Color.CYAN);
        }
        return true;
    }
}
  1. Begin by making a new project.
  2. Using the activity main.xml file, open res -> Layout -> activity main.xml and enter the code below. Add only an Adapted version to this file to showcase a simple text.
  3. Access the software -> Java -> Package -> Mainactivity.java file to begin working with it. Add this same code to display the ContextMenu in this step. When the app starts, perform a long button on a text and it will showcase the variety of choices to choose from for specific purposes.
  4. To begin coding, we start creating an import function in Java.
  5. We can associate those items with the ids we specified in the.XML file.
  6. Next, you must register a view for the context menu; you can register any view, such as a listview, image view, textview, button, and so on.
  7. After that, you can customise the menu header with a title icon as well as add menu items. Then, under the menu item, we select listener.

Conclusion :-

The contextual response mode is an ActionMode system implementation that directs users to interact forward into performing contextual actions.

When a user activates this method by identifying a product, a contextual activity bar has seemed at the top of the screen, displaying actions that the user can take with the currently selected item (s).

I hope this article on contextual menu android helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪