ContextMenu with Header in Android

September 24, 2014

Download project here : ContextMenuwithHeader.zip

for context menu options create menu.xml file under menu folder under res folder
 menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/item_option1"
        android:title="Option1">
    </item>
    <item
        android:id="@+id/item_option2"
        android:title="Option2">
    </item>
    <item
        android:id="@+id/item_option3"
        android:title="Option3">
    </item>
    <item
        android:id="@+id/item_option4"
        android:title="Option4">
    </item>
    <item
        android:id="@+id/item_option5"
        android:title="Option5">
    </item>

</menu>


activity_main.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="${packageName}.${activityClass}" >

    <Button
        android:id="@+id/button_longPress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="144dp"
        android:text="Long press here" />

</RelativeLayout>


MainActivity.java:
package com.ram.customcontextmenu;

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.Button;
import android.widget.Toast;

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Calling button reference from xml layout
        Button contextMenu_button = (Button) findViewById(R.id.button_longPress);

        // registering button for the context menu
        registerForContextMenu(contextMenu_button);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);
        // setting header title for the context menu
        menu.setHeaderTitle("Select an Option");
        // Setting header icon
        menu.setHeaderIcon(R.drawable.ic_launcher);
        // Inflating menu.xml file from menu folder
        getMenuInflater().inflate(R.menu.menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        // To listen action for the context menu options
        switch (item.getItemId())
        {
        case R.id.item_option1:
            Toast.makeText(getApplicationContext(), "option1 selected",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.item_option2:
            Toast.makeText(getApplicationContext(), "option2 selected",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.item_option3:
            Toast.makeText(getApplicationContext(), "option3 selected",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.item_option4:
            Toast.makeText(getApplicationContext(), "option4 selected",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.item_option5:
            Toast.makeText(getApplicationContext(), "option5 selected",
                    Toast.LENGTH_SHORT).show();
            break;
        }
        return super.onContextItemSelected(item);

    }
}
 

Artikel Terkait

Previous
Next Post »