Opening a floating menu (context menu) in Android?

Mohammad picture Mohammad · Oct 1, 2014 · Viewed 10.9k times · Source

I created a new menu, named drmenu.xml. It works correctly when i press menu button, but I need to open a context menu when the user press the button. The below code the button just show a toast.

this is my xml layout:

 <LinearLayout
        android:id="@+id/MenuCall"
        android:layout_width="90dip"
        android:layout_height="match_parent"
        android:gravity="right|center_vertical" >
        <ImageView
            android:id="@+id/MenuCall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/imageiew6" />
    </LinearLayout>

and this is my java code:

    final LinearLayout callback_var = (LinearLayout) findViewById(R.id.MenuCall);
    registerForContextMenu(callback_var);
    callback_var.setOnClickListener(new android.view.View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "this is repeated",      Toast.LENGTH_LONG).show();
            openContextMenu(callback_var);
        }

Answer

GVillani82 picture GVillani82 · Oct 1, 2014

If you want create a context Menu, you have to call the method registerForContextMenu() passing it the View that should be associated with the context menu.

For example, supposing to associate the context menu with a Button:

Button button = (Button) findViewById(R.id.my_button);
registerForContextMenu(button);

which can be called in the onCreate() of your Activity. Then, inside the same activity, you need to override the onCreateContextMenu() method.

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.your_context_menu, menu);
}

Then you have to implement onContextItemSelected(), for triggering the proper action when a item inside the context menu is pressed:

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.first_action:
            // your first action code
            return true;
        case R.id.second_action:
            // your second action code
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

Now the long press click on the button opens the context menu you defined in the your_context_menu.xml file.

Consider that opening the context menu with a long press is compliant with the Android standard UI, however If you want your context menu to appear on a simple tap you can look here the answer

NOTE: As said here

An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).