Tuesday, November 22, 2011

Creating a Context Menu

In this post I show how to create a Context Menu in android.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image" />

</LinearLayout>

ContextMenuActivity.java

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

public class ContextMenuActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        ImageView imageView=(ImageView)findViewById(R.id.imageView1);
        /* to register a context menu for imageView.
         * Because multiple views can show the context menu.
         */
        registerForContextMenu(imageView);
    }

   
    /*
     * This method is called when a context menu for the view about to be shown.
     */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
       
        super.onCreateContextMenu(menu, v, menuInfo);
        if(v.getId()==R.id.imageView1)
        {
    // to set the context menu's header icon
        menu.setHeaderIcon(R.drawable.iconimage);
       
    // to set the context menu's title
        menu.setHeaderTitle("Context Menu");
       
    // to add a new item to the menu   
        menu.add(0,0,0, "Save");
        menu.add(0,1,0,"Delete");
        }
       
    }
    /*
     * This method is called when an item in a context menu is selected.
     *
     */

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId())
        {
        case 0:
            save(item.getItemId());
            break;
        case 1:
            delete(item.getItemId());
            break;
        }
        return true;
    }
   
    private void delete(int itemId) {
        Toast.makeText(this, "You pressed Delete", Toast.LENGTH_LONG).show();
       
    }


    public void save(int i)
    {
        Toast.makeText(this, "You pressed Save", Toast.LENGTH_LONG).show();
    }
   
   
}

Before run this application put two images "image" and "iconimage" into your drawable folder. Here "image" will be displayed in the ImageView and "iconimage" will be displayed in the context menu header.

Now run the project and you will get the out put as follows..




















Long press on the image to view the context menu..



1 comment:

  1. Hello. I am a new guy to android. what should i do if i want to delete the selected image only from the view and also from the memory

    ReplyDelete