Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open ContextMenu on single click

I am trying to add a context menu to a linear layout and open it on single click. The linear layout, in turn, contains a textview so that the listener can be invoked when user clicks anywhere on the layout.

Setting onClick listener to linear layout.

linearLayout.setOnClickListener(this);

And the onClick method:

public void onClick(View view) {
    openContextMenu(view);
    //view.showContextMenu(); // didn't work as well
}

the above method should trigger the following

public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo){            
     super.onCreateContextMenu(menu, view, menuInfo);  
     menu.setHeaderTitle("Title");
     menu.add(0, id, 0, "Item 1");
}

However, this seems to be not working. From other community posts in here, this is the solution I could possibly find. I wonder if I miss something.

Any thoughts?

Thanks in advance!

like image 811
Renjith Avatar asked Jan 25 '26 20:01

Renjith


1 Answers

Did you register the context menu for the linear layout using:

registerForContextMenu(linearLayout);
like image 54
Nermeen Avatar answered Jan 27 '26 11:01

Nermeen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!