What is context menu

Android context menu is a floating menu it is display when the user make long press on the view like button etc.It perform the action that affact the selected context frame. In my program the user make long press over the button it will display the context menu.

In my program i am going to create a simple context menu when you long press onto the button it will display the context menu.


Context Menu:.

In this section i am going to show the context menu with practical example.

First open the android studio and create a project and give the name of your project like ContextMenuDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop one button from pallet on to the MainActvity.

Right click on res folder and a pop up menu will display click on new and then select the Android resource file a dialog box will display where you need to write your file name(main_menu) and Resource type as Menu.Figure display all the info do according the figure.

Smiley face

Open the main_menu.xml file and code look like this.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/item1"
    android:title="Privacy"
    app:showAsAction="never">

</item>
    <item
        android:id="@+id/item2"
        android:title="Restart"
        app:showAsAction="never">

    </item>
    <item
        android:id="@+id/item3"
        android:title="Custom Chats"
        app:showAsAction="never">

    </item>
    <item
        android:id="@+id/item4"
        android:title="Message Schdeular"
        app:showAsAction="never">

    </item>
</menu>

       

Open the MainActivity.java file and code look like this.

package com.example.niitpc.contextmenudemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
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 onContextMenu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onContextMenu=(Button)findViewById(R.id.button);
        registerForContextMenu(onContextMenu);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        
        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.main_menu,menu);
        super.onCreateContextMenu(menu, v, menuInfo);


    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.item1:
                Toast.makeText(getApplicationContext(),"Privacy selected",Toast.LENGTH_LONG).show();

            case R.id.item2:
                Toast.makeText(getApplicationContext(),"Restart selected",Toast.LENGTH_LONG).show();

            case R.id.item3:
                Toast.makeText(getApplicationContext(),"Custom Chats selected",Toast.LENGTH_LONG).show();

            case R.id.item4:
                Toast.makeText(getApplicationContext(),"Message Schedular selected",Toast.LENGTH_LONG).show();


        }
        return super.onContextItemSelected(item);
    }


}

       


Explanation:

Button onContextMenu:Here i am going to declare the onContextMenu object of the Button.

onCreateOptionsMenu(Menu menu):This method is responsible for creating option menu in android.

onContextMenu=(Button)findViewById(R.id.button):This statement initialized the onContextMenu object.

registerForContextMenu(onContextMenu):The registerForContextMenu register the view for context menu here view is butoon(onContextMenu).

MenuInflater menuInflater=getMenuInflater();:This statement initialized the object menuInflater of MenuInflate by using getMenuInflater().

menuInflater.inflate(R.menu.main_menu,menu):With the help of menuInflater object we call the inflate method that take one argument that inflat the menu.

onContextItemSelected(MenuItem item):This method is responsible for selection of context menu items it has a argument that returns the selected item id.

switch (item.getItemId()):With in onContextItemSelected(MenuItem item) i am using the switch case for matching particular item id and display a little message with the help of Toast.


Context Menu

What is context menu for video Click here

Smiley face                     Smiley face