Tuesday, March 16, 2021

Android Context Menu

 

Android Context Menu Example

Android context menu appears when user press long click on the element. It is also known as floating menu.

It affects the selected content while doing action on it.

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. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.contextmenu.MainActivity">  
  8.   
  9.     <ListView  
  10.         android:layout_width="368dp"  
  11.         android:layout_height="495dp"  
  12.         android:id="@+id/listView"  
  13.         android:layout_marginEnd="8dp"  
  14.         android:layout_marginStart="8dp"  
  15.         android:layout_marginTop="8dp"  
  16.         app:layout_constraintEnd_toEndOf="parent"  
  17.         app:layout_constraintHorizontal_bias="0.0"  
  18.         app:layout_constraintStart_toStartOf="parent"  
  19.         app:layout_constraintTop_toTopOf="parent" />  
  20. </android.support.constraint.ConstraintLayout>  


main_menu.xml

Create a separate menu_main.xml file in menu directory for menu items.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:id="@+id/call"  
  4.         android:title="Call" />  
  5.     <item android:id="@+id/sms"  
  6.         android:title="SMS" />  
  7. </menu>  

Activity class

Let's write the code to display the context menu on press of the listview.

File: MainActivity.java
  1. package example.javatpoint.com.contextmenu;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.ContextMenu;  
  6. import android.view.MenuInflater;  
  7. import android.view.MenuItem;  
  8. import android.view.View;  
  9. import android.widget.ArrayAdapter;  
  10. import android.widget.ListView;  
  11. import android.widget.Toast;  
  12.   
  13. public class MainActivity extends AppCompatActivity {  
  14.     ListView listView;  
  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.         listView=(ListView)findViewById(R.id.listView);  
  21.         ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contacts);  
  22.         listView.setAdapter(adapter);  
  23.         // Register the ListView  for Context menu  
  24.         registerForContextMenu(listView);  
  25.     }  
  26.     @Override  
  27.     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)  
  28.     {  
  29.         super.onCreateContextMenu(menu, v, menuInfo);  
  30.         MenuInflater inflater = getMenuInflater();  
  31.         inflater.inflate(R.menu.menu_main, menu);  
  32.         menu.setHeaderTitle("Select The Action");  
  33.     }  
  34.     @Override  
  35.     public boolean onContextItemSelected(MenuItem item){  
  36.         if(item.getItemId()==R.id.call){  
  37.             Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();  
  38.         }  
  39.         else if(item.getItemId()==R.id.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

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile