Hello all
I think you all know about the context menu in android. It appears as a popup when you long press something. Fragment also provides some methods for registering with the contextmenu and showing it.
Let’s see how we can do it.
You can click on the download links to download the source code.
At first we will look at the simple layout for demonstrating this example.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/fragment_context_menu_msg" /> <Button android:id="@+id/long_press" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Long Press Me"> <requestFocus /> </Button> </LinearLayout>
Now create a folder named “menu” inside the res folder inside your project.
And create an xml file named “my_menu.xml” inside this folder and copy this code into it.
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/invisible_item" android:visible="false" android:alphabeticShortcut="i" android:title="Invisible item" /> <item android:id="@+id/a_item" android:alphabeticShortcut="a" android:title="Alvin" /> <item android:id="@+id/b_item" android:alphabeticShortcut="b" android:title="Bart" /> <item android:id="@+id/c_item" android:alphabeticShortcut="c" android:title="Chris" /> <item android:id="@+id/d_item" android:alphabeticShortcut="d" android:title="David" /> <item android:id="@+id/e_item" android:alphabeticShortcut="e" android:title="Eric" /> <item android:id="@+id/f_item" android:alphabeticShortcut="f" android:title="Frank" /> <item android:id="@+id/g_item" android:alphabeticShortcut="g" android:title="Gary" /> <item android:id="@+id/h_item" android:alphabeticShortcut="h" android:title="Henry" /> <item android:id="@+id/excl_item" android:alphabeticShortcut="!" android:title="Exclamation" /> </menu>
I am using only two of the items from the above XML for showing the context menu.
Now we will go to the java code. My Activity is named FragmentContextMenu.java which looks like this.
package com.coderzheaven.fragmentcontextmenu; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; /** * Demonstration of displaying a context menu from a fragment. */ public class FragmentContextMenu extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create the list fragment and add it as our sole content. ContextMenuFragment content = new ContextMenuFragment(); getFragmentManager().beginTransaction().add(android.R.id.content, content).commit(); } public static class ContextMenuFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragment_context_menu, container, false); registerForContextMenu(root.findViewById(R.id.long_press)); return root; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(Menu.NONE, R.id.a_item, Menu.NONE, "Menu A"); menu.add(Menu.NONE, R.id.b_item, Menu.NONE, "Menu B"); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.a_item: Log.i("ContextMenu", "Item 1a was chosen"); return true; case R.id.b_item: Log.i("ContextMenu", "Item 1b was chosen"); return true; } return super.onContextItemSelected(item); } } }
And That’s all done. Now you can run the project and see the result.
You can download the complete source code from here.