How to show the context menu on an inflated view?

I want to display a context menu for a bloated view. Here is a sample code:

for grid_layout.xml:

<?xml version="1.0" encoding="utf-8"?>    
    <ImageView
            xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:antialias="true" />

Now I use it in my activiy class as:

  @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {       
            menu.setHeaderTitle("Select action");       
            menu.add(0, 1, 0, "Action1");
            menu.add(0, 2, 0, "Action2");

            super.onCreateContextMenu(menu, v, menuInfo);
    }

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    ImageView imageView = (ImageView) inflater.inflate(R.layout.grid_layout, null);
    imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               registerForContextMenu(v);
                       openContextMenu(v);
            }

        });

This code runs without any errors, but the context menu does not appear when I click on the image. Is there something wrong with this code?

+3
source share
3 answers

I found a workaround for this situation and solved my problem. Since I said that the same code works fine and displays ContextMenu if I define ImageView in XML, which I set in the setContentView () method. I just used the object of this imageView to register contextMenu and displayed ContextMenu when I clicked on the bloated element. Here is a sample code:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {       
            menu.setHeaderTitle("Select action");       
            menu.add(0, 1, 0, "Action1");
            menu.add(0, 2, 0, "Action2");

            super.onCreateContextMenu(menu, v, menuInfo);
    }

    ImageView imageViewInContext = (ImageView) findViewById(R.id.imageview_in_main_xml);
    registerForContextMenu(imageViewInContext);

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    ImageView imageView = (ImageView) inflater.inflate(R.layout.grid_layout, null);
    imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   
                 openContextMenu(imageViewInContext);
            }

        });

, -!

+4

:

imageView.setFocusable(true);
imageView.setClickable(true);

Imageview clickEvent.

+1

Since you create an anonymous inner class ( new View.OnClickListenerwhile doing ), you no longer work in the UI thread (your activity class), so the context menu does not load if you want registerForContextMenuand openContextMenu. You can use Handlerto send a message to the user interface thread (your activity class) to perform these actions, or try to reference your Activity class in the inner class. Something like that:

activityClassName.this.registerForContextMenu(v);
activityClassName.this.openContextMenu(v);
0
source

All Articles