AdSense

AdSense3

Monday 17 August 2015

Android Context Menu Example

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 Example

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

activity_main.xml

Drag one listview from the pallete, now the xml file will look like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <ListView  
  12.         android:id="@+id/listView1"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_alignParentTop="true"  
  17.         android:layout_marginLeft="66dp"  
  18.         android:layout_marginTop="53dp" >  
  19.     </ListView>  
  20.   
  21. </RelativeLayout>  

Activity class

Let's write the code to display the context menu on press of the listview.
File: MainActivity.java
  1. package com.javatpoint.contextmenu;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.ContextMenu;  
  5. import android.view.ContextMenu.ContextMenuInfo;  
  6. import android.view.Menu;  
  7. import android.view.MenuItem;  
  8. import android.view.View;  
  9. import android.widget.AdapterView;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.ListView;  
  12. import android.widget.Toast;  
  13. public class MainActivity extends Activity {  
  14.     ListView listView1;  
  15.     String contacts[]={"Ajay","Sachin","Sumit","Tarun","Yogesh"};  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         listView1=(ListView)findViewById(R.id.listView1);  
  21.         ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contacts);  
  22.         listView1.setAdapter(adapter);  
  23.         // Register the ListView  for Context menu  
  24.         registerForContextMenu(listView1);  
  25.     }  
  26.     @Override   
  27.     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)  
  28.     {  
  29.             super.onCreateContextMenu(menu, v, menuInfo);  
  30.             menu.setHeaderTitle("Select The Action");    
  31.             menu.add(0, v.getId(), 0"Call");//groupId, itemId, order, title   
  32.             menu.add(0, v.getId(), 0"SMS");   
  33.     }   
  34.     @Override    
  35.     public boolean onContextItemSelected(MenuItem item){    
  36.             if(item.getTitle()=="Call"){  
  37.                 Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();  
  38.             }    
  39.             else if(item.getTitle()=="SMS"){  
  40.                 Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();  
  41.             }else{  
  42.                return false;  
  43.             }    
  44.           return true;    
  45.       }    
  46.     }  

Output:

android context menu example output 1

Output after long press on the listview.
android context menu example output 2

Output after clicking on the context menu.
android context menu example output 3

No comments:

Post a Comment