All TalkersCode Topics

Follow TalkersCode On Social Media

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

Context Menu In Android

Last Updated : Mar 11, 2024

Context Menu In Android

In this article we will show you the solution of context menu in android, when a user long holds or clicks on an item, an Android context menu, which resembles a floating menu, emerges.

This feature is helpful for trying to build features that describe the specific data or stationary reference effect.

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

A context menu can be supplied for every view in the Android system and will offer activities that change a particular component or context sequence in the user interface.

The context menu will not support object shortcuts or object icons. We should mention that Java will be used to implement this project.

In many different applications, menu items are a common user integration component.

You should utilize the Menu APIs to display user interactions and other choices available in your activities to create a commonly associated and consistent user experience.

Operating systems such as ios equipment are no longer required to have a dedicated Menu button as of Android 3.0 (API level 11).

With for this change, Android apps should no longer rely on the conventional 6-item menu panel, instead providing an app bar to display common user actions.

That although design and user experience of some menu items have changed, the Menu APIs are still used to describe a set of actions and options.

A popup menu demonstrates a collection of options in a linear list that is anchored to the view from which the menu was invoked.

It's useful for providing a flood of actions associated with specific content or for providing options for the second part of a prompt.

Contextual actions should be used for actions in a popup menu that do not actually influence the corresponding content.

Somewhat more, the popup menu is for extensive actions related to content regions in your activity.

For all sorts of menus, Android provides a standard XML format for creating menu items. Instead of creating a menu in the code of your activity, describe a menu and all of the components in an XML menu source of energy.

The menu resource can then be inflated (loaded as a Menu element) in your interaction or fragment.

Step By Step Guide On Context Menu In Android :-

Xml code

<?xml version="1.0" encoding="utf-8"?>
<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 Main extends CompatActivity{
    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. Proceed a New Project.
  2. To work also with activity main.xml file, navigate to res -> Layout -> activity main.xml and enter the following code. Add even just a Text View to this file to showcase simple text.
  3. To work with the Mainactivity.java file, navigate to app -> Java -> Package -> Mainactivity.java. Add the code to display the Context Menu in this step. When the app starts, execute a long click on such a text and it will screen the variety of options to choose from for specific purposes.
  4. To begin coding, designers create a value chain in Java.
  5. We can associate those components with the ids we specified in the.XML file.
  6. Next, you must register a vision for the context menu, you can register any view, such as a listview, image view, textview, key on the keyboard, and so on.
  7. After that, you can customise the menu header with a title icon and add menu items.
  8. Afterward, under the menu item, authors select listener.

Conclusion :-

The context menu will not support object shortcuts or object icons. We should mention that Java will be used to implement this project.

In many different applications, menu items are a common user integration component.

You should utilize the Menu APIs to display user interactions and other choices available in your activities to create a commonly associated and consistent user experience.

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

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪