context menu

Context Menu

Android Menu is an easy and flexible user interface component, which is used to handle a set of actions. Context menu povides a set of menu options when user perform long click on an Element. Most often context menu is used for items in a RecyclerView, GridView, or other view collections in which the user can perform direct actions on each item.

Android provides two different way of context menu :-

In this article we will only discuss about android floating menu and floating menu examples.

Floating Menu :

A floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or a view element on the screen. It provides a floating list of menu items when the user performs a long tap on an item/element.

1. Create a New Project :

Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from templates to create an Floating menu in android using android studio.

2. Add Button to your activity_main.xml or main layout

Add Button to your main layout. 

<Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Context Menu"/>

Now open res -> layout -> activity_main.xml  file and just implement the following xml code .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Context Menu"/>

</RelativeLayout>

3. Define a XML menu resource file :

Create a menu resource file named as options_menu.xml :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/notification"
        android:title="@string/notification"/>
    <item android:id="@+id/help"
        android:title="@string/help"/>
    <item android:id="@+id/setting"
        android:title="@string/setting"/>
    <item android:id="@+id/logout"
        android:title="@string/logout"/>
</menu>

4. Registering a view to the context menu or Floating menu :

Register a view to the context menu or Floating menu by calling the registerForContextMenu() method and passing it the view.

registerForContextMenu(btn);

5. Inflating the menu :

To inflate the menu resource (defined in XML) into the Menu override the onCreateContextMenu() method in your activity or fragment.

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.options_menu,menu);

    }

6. Handling menu item clicks :

When the user selects an item from the floating menu or context menu, the system calls activity’s onContextItemSelected() method. The onContextItemSelected() method can handle all the menu item clicks in one place, and determine which menu item was clicked.

@Override
    public boolean onContextItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()){
            case R.id.notification :
                Toast.makeText(this,"Notification", Toast.LENGTH_SHORT).show();
                break;
            case R.id.help :
                Toast.makeText(this,"Help",Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting :
                Toast.makeText(this,"Setting",Toast.LENGTH_SHORT).show();
                break;
            case R.id.logout :
                Toast.makeText(this,"Logout",Toast.LENGTH_SHORT).show();
                break;
            default:
                break;

        }
        return super.onContextItemSelected(item);
    }

7. Now open your MainActivity.java and add following code :

package net.technxt.contextmenu;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button)findViewById(R.id.btn);
        registerForContextMenu(btn);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.options_menu,menu);

    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // Handle item click
        switch (item.getItemId()){
            case R.id.notification :
                Toast.makeText(this,"Notification", Toast.LENGTH_SHORT).show();
                break;
            case R.id.help :
                Toast.makeText(this,"Help",Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting :
                Toast.makeText(this,"Setting",Toast.LENGTH_SHORT).show();
                break;
            case R.id.logout :
                Toast.makeText(this,"Logout",Toast.LENGTH_SHORT).show();
                break;
            default:
                break;

        }
        return super.onContextItemSelected(item);
    }
}

Now run the project/app to test Floating Menu in android app through android device or any Emulator .

%d bloggers like this: