Thursday, 16 August 2012

Context Menu Example in Android

Context Menu is a floating list of menu items that appears when a user touches
and holds a particular item displayed in the view,which has a menu associated with it.

Here below example shows ContextMenu when user Long_press on one Button.

UI Layout(android_context_menu_example.xml)


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AndroidContextMenuExample" >

    <Button
        android:id="@+id/click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Long-press on me i will show Context Menu.." />

</RelativeLayout>

Android Activity (AndroidContextMenuExample.java)

package com.androidsurya.androidcontextmenu;

import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;

public class AndroidContextMenuExample extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.android_context_menu_example);

        Button click = (Button) findViewById(R.id.click);
        registerForContextMenu(click);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Context Menu Title");
        menu.add(0, v.getId(), 0, "Item 1");
        menu.add(0, v.getId(), 0, "Item 2");
        menu.add(0, v.getId(), 0, "Item 3");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // when ever user Click on any item Toast message will fired..for Demo
        // purpose i added Toast message now you can add your functionality
        if (item.getTitle() == "Item 1") {
            Toast.makeText(getApplicationContext(), "Item 1 clicked....",
                    Toast.LENGTH_LONG).show();
        } else if (item.getTitle() == "Item 2") {
            Toast.makeText(getApplicationContext(), "Item 2 clicked....",
                    Toast.LENGTH_LONG).show();
        } else if (item.getTitle() == "Item 3") {
            Toast.makeText(getApplicationContext(), "Item 3 clicked....",
                    Toast.LENGTH_LONG).show();
        } else {
            return false;
        }
        return true;
    }

}

Register Activity in AndroidManifest file

 <activity
            android:name="com.androidsurya.androidcontextmenu.AndroidContextMenuExample"
            android:label="@string/app_name" >

Output Screenshot











For More information about Context Menu : Android Developers site

No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...