Creating a floating contextual menu in Android

July 24, 2012
By

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”

New Android Project

Figure 1 : New Android Project


2. Select Android build target

Select Build Target

Figure 2 : Select Build Target


3.Enter application details

Enter application Details

Figure 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

Main Activity

Figure 4 : Main Activity

Long Pressing an item in the ListView

Figure 5 : Long Pressing an item in the ListView

Selecting a menu item from the context menu

Figure 6 : Selecting a menu item from the context menu

Shows a Toast message when a menu item is selected

Figure 7 : Shows a Toast message when a menu item is selected


8. Download


9. Reference

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


How to hire me?

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


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , , ,

2 Responses to Creating a floating contextual menu in Android

  1. Nihal Srivastava on February 12, 2015 at 6:10 pm

    Please tell me complete logic to delete item from LilstView Using Context Menu

  2. Nauman on January 3, 2016 at 1:22 am

    Great work……..Thankx……………….Keep it up…………..

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner