In this article we will see how to create a floating contextual menu in an Android application.In the main activity, a list of country names will be listed in listview. On long pressing an item, the contextual menu will be appeared. There will be three menu items in the context menu such as “Edit”, “Share” and “Delete”. On selecting a menu item, the selected menu and country name will be displayed in a Toast message.
This application is developed in Eclipse 3.7.2 and tested in Android API level 10.
1. Create a new Android project namely “FloatingContextualMenu”
2. Select Android build target
3.Enter application details
4. Update the file res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">FloatingContextualMenu</string> <string name="str_cnt_mnu_edit">Edit</string> <string name="str_cnt_mnu_share">Share</string> <string name="str_cnt_mnu_delete">Delete</string> </resources>
5. Create a menu file res/menu/actions.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/cnt_mnu_edit" android:title="@string/str_cnt_mnu_edit" /> <item android:id="@+id/cnt_mnu_share" android:title="@string/str_cnt_mnu_share" /> <item android:id="@+id/cnt_mnu_delete" android:title="@string/str_cnt_mnu_delete" /> </menu>
6. Update src/in/wptrafficanalyzer/floatingcontextualmenu/MainActivity.java
package in.wptrafficanalyzer.floatingcontextualmenu; import android.app.ListActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ArrayAdapter; import android.widget.Toast; public class MainActivity extends ListActivity { String[] countries = new String[] { "India", "Pakistan", "Bangladesh", "Sri Lanka", "Nepal", "Bhutan", "Burma", "China" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries); getListView().setAdapter(adapter); /** Registering context menu for the listview */ registerForContextMenu(getListView()); } /** This will be invoked when an item in the listview is long pressed */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.actions , menu); } /** This will be invoked when a menu item is selected */ @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch(item.getItemId()){ case R.id.cnt_mnu_edit: Toast.makeText(this, "Edit : " + countries[info.position] , Toast.LENGTH_SHORT).show(); break; case R.id.cnt_mnu_delete: Toast.makeText(this, "Delete : " + countries[info.position] , Toast.LENGTH_SHORT).show(); break; case R.id.cnt_mnu_share: Toast.makeText(this, "Share : " + countries[info.position] , Toast.LENGTH_SHORT).show(); break; } return true; } }
7. Application in execution
8. Download

9. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com
Please tell me complete logic to delete item from LilstView Using Context Menu
Great work……..Thankx……………….Keep it up…………..