Android tutorial - Android Context Menu - android app development - android studio - android development tutorial


android programming - android software - android emulator

What is android context menu?

 cm2
  • It can be seen in Android main screen, when you long press/click the screen it will get activated. Android context menu is similar to context menu in Windows or Linux, the one which is displayed on right click.
  • Now let’s see an example to create Context menu in Android.
  • Android context menu appears when user press long click on the element. It is also known as floating menu.
  • It doesn't support item shortcuts and icons.
  • android context menu sample code

Android Context Menu Example

  • Let's see the simple example of context menu in android.
android development tutorial

activity_main.xml

  • Drag one listview from the pallete, now the xml file will look like this:
  • File: activity_main.xml
android codes - android tutorial - android sdk
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  
  
    <ListView  
        android:id="@+id/listView1"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="66dp"  
        android:layout_marginTop="53dp" >  
    </ListView>  
  
</RelativeLayout>  
click below button to copy the code from android tutorial team

Activity class

  • Let's write the code to display the context menu on press of the listview.
  • File: MainActivity.java
android codes - android tutorial - android sdk
package com.wikitechy.contextmenu;  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.ContextMenu;  
import android.view.ContextMenu.ContextMenuInfo;  
import android.view.Menu;  
import android.view.MenuItem;  
import android.view.View;  
import android.widget.AdapterView;  
import android.widget.ArrayAdapter;  
import android.widget.ListView;  
import android.widget.Toast;  
public class MainActivity extends Activity {  
    ListView listView1;  
    String contacts[]={"HTML","CSS","PHP","Angular","Android"};  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        listView1=(ListView)findViewById(R.id.listView1);  
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contacts);  
        listView1.setAdapter(adapter);  
        // Register the ListView  for Context menu  
        registerForContextMenu(listView1);  
    }  
    @Override   
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)  
    {  
            super.onCreateContextMenu(menu, v, menuInfo);  
            menu.setHeaderTitle("Select The Action");    
            menu.add(0, v.getId(), 0, "View");//groupId, itemId, order, title   
            menu.add(0, v.getId(), 0, "Delete");   
    }   
    @Override    
    public boolean onContextItemSelected(MenuItem item){    
            if(item.getTitle()=="View"){  
                Toast.makeText(getApplicationContext(),"viewing…",Toast.LENGTH_LONG).show();  
            }    
            else if(item.getTitle()=="Delete"){  
                Toast.makeText(getApplicationContext(),"Deleteing…",Toast.LENGTH_LONG).show();  
            }else{  
               return false;  
            }    
          return true;    
      }    
    }  
click below button to copy the code from android tutorial team
  • download this android example

Output:

 context1
  • Output after long press on the listview.
 context2
  • Output after clicking on the context menu.
 context3

Related Searches to Android Context Menu