What is ContextMenu ?
ContextMenu is a floating menu.when user perform a long press on element and it is display to selected option of context menu. It affects the selected content while doing action on it.It doesn’t support item shortcuts and icons.
Windows os system when user right click to open refresh menu same like context menu when user long press to open menu and display item when user selected to item action performed.
Context menu need to register view on activity or fragment to used this method of registerForContextMenu(View) . And we need to override onCreateContextMenu() in our activity or fragments.
Implementation of ContextMenu
Create activity_main.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_context_menu" 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" android:orientation="vertical" tools:context="com.bhaumik.programmingstudy.MainActivity"> <ListView android:layout_marginTop="15dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/list_view"></ListView> </LinearLayout> |
Create MainActivity.java File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.bhaumik.programmingstudy; 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.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ListView listView; ArrayAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.list_view); adapter = ArrayAdapter.createFromResource(this,R.array.str_language,android.R.layout.simple_list_item_1); listView.setAdapter(adapter); registerForContextMenu(listView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("Select The Action"); menu.add(0,v.getId(),0,"Remove Item"); menu.add(0,v.getId(),0,"Hide Item"); menu.add(0,v.getId(),0,"Show Item"); } @Override public boolean onContextItemSelected(MenuItem item) { if (item.getTitle() == "Remove Item") { Toast.makeText(this,"Remove Item Clicked",Toast.LENGTH_SHORT).show(); } else if (item.getTitle() == "Hide Item") { Toast.makeText(this,"Hide Item Clicked",Toast.LENGTH_SHORT).show(); } else if (item.getTitle() == "Show Item") { Toast.makeText(this,"Show Item Clicked",Toast.LENGTH_SHORT).show(); } return true; } } |
Now in this code i have used to list view and list of item add string.when user click of any item long press to open context menu dialog and selected any action item in context menu.