Saturday, October 5, 2019

Android Context Menu with Examples

In android, Context Menu is like a floating menu and that appears when the user performs a long press or click on an element and it is useful to implement an actions that effect the selected content or context frame.

The android Context Menu is more like the menu which displayed on right click in Windows or Linux.

Following is the pictorial representation of using Context Menu in our android applications.

Android Context Menu Example Diagram

In android, the Context Menu offers an actions that effect a specific item or context frame in the UI and we can provide a context menu for any view. The context menu won’t support any item shortcuts and item icons.

Create Android Context Menu in Activity

The views which we used to show the context menu on long press, we need to register that views using registerForContextMenu(View) in our activity and we need to override onCreateContextMenu() in our activity or fragment.

When the registered view receives a long click event, the system calls our onCreateContextMenu() method. By using onCreateContextMenu() method, we can create our menu items like as shown below.

@Overrideprotected void onCreate(Bundle savedInstanceState) {
    
super.onCreate(savedInstanceState);
    setContentView(R.layout.
activity_main);
    Button btn = (Button) findViewById(R.id.
btnShow);
    registerForContextMenu(btn);
}
@Overridepublic void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    
super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle(
"Context Menu");
    menu.add(
0, v.getId(), 0"Upload");
    menu.add(
0, v.getId(), 0"Search");  
}
If you observe above code, we registered our Button control using registerForContextMenu() to show the context menu on button long-click and binding the Context Menu items using onCreateContextMenu() method.

Handle Android Context Menu Click Events

In android, we can handle a context menu item click events using onContextItemSelected() method. 

Following is the example of handling a context menu item click event using onContextItemSelected() method.

@Overridepublic boolean onContextItemSelected(MenuItem item) {
    
if (item.getTitle() == "Save") {
        
// do your coding
    
}
    
else {
        
return  false;
    }
    
return true;
}
Note: If you are using Android 3.0 +, the Context Menu won’t support any item shortcuts and item icons in the menu.

Android Context Menu Example

Following is the example of implementing a Context Menu in android application.

Create a new android application using android studio and give names as ContextMenuExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.

Now open an activity_main.xml file from \res\layout path and write the code like as shown below

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
android:layout_width="match_parent"
    
android:layout_height="match_parent"
    
android:orientation="vertical" >
    <
Button
        
android:id="@+id/btnShow"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:text="Long press me"
        
android:layout_marginTop="200dp" android:layout_marginLeft="100dp"/>
</
LinearLayout>
If you observe above code we created a one Button control in XML Layout file to show the context menu when we do long press on Button

Once we are done with creation of layout with required control, we need to load the XML layout resource from our activity onCreate() callback method, for that open main activity file MainActivity.java from \java\com.tutlane.contextmenuexample path and write the code like as shown below.

MainActivity.java

package com.tutlane.contextmenuexample;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.ContextMenu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    
@Override
    
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Button btn = (Button) findViewById(R.id.
btnShow);
        registerForContextMenu(btn);
    }
    
@Override
    
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        
super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle(
"Context Menu");
        menu.add(
0, v.getId(), 0"Upload");
        menu.add(
0, v.getId(), 0"Search");
        menu.add(
0, v.getId(), 0"Share");
        menu.add(
0, v.getId(), 0"Bookmark");
    }
    
@Override
    
public boolean onContextItemSelected(MenuItem item) {
        Toast.makeText(
this"Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
        
return true;
    }
}
If you observe above code we are overriding onCreateContextMenu() method in activity to create context menu and registered view for context menu using registerForContextMenu().

Generally, during the launch of our activityonCreate() callback method will be called by android framework to get the required layout for an activity.

Output of Android Context Menu Example

When we run above example using android virtual device (AVD) we will get a result like as shown below.

Android Context Menu Example Result

This is how we can create Context Menu in android applications to show the menu list when the user long press on defined element in our application.

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...