I have a problem with the contextual menu, I have a control "ImageButton" when you make a long click displays a context menu.
I need to display the contextual menu with a short click, not a long click, is this possible?
This is the code I currently use, the menu works perfectly.
private ImageView btnRutas;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fclientes);
btnRutas =(ImageView)findViewById(R.id.btnRutas);
btnRutas.setOnClickListener(this);
registerForContextMenu(btnRutas);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
if(v.getId()== R.id.btnRutas) {
menu.setHeaderIcon(android.R.drawable.ic_menu_more);
menu.setHeaderTitle("Rutas");
menu.add(0, 0, 0, "Ruta 1");
}
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case 0:
function();
}
}
Thanks in advance.
A context menu (also know as a contextual menu, shortcut menu or pop-up menu) is the menu that appears when you right-click and offers a set of choices that are available for, or in context of, whatever it was you clicked. The available choices are usually actions specifically related to the selected object.
The context menu (right-hand mouse button or SHIFT+F10 ) allows you to display a context-sensitive menu. The context is defined by the position of the mouse pointer when the user requests the menu. A context menu allows the user to choose functions that are relevant to the current context.
A context menu (also called contextual, shortcut, and pop up or pop-up menu) is a menu in a graphical user interface (GUI) that appears upon user interaction, such as a right-click mouse operation.
A context menu offers a list of relevant commands that apply to the current selection or task. The pen user can select commands near the selected object rather than move his or her hand across the screen to the menu or toolbar.
I think you should use popup menu instead of context menu. Check documentation https://developer.android.com/guide/topics/ui/menus, or do it like this:
private void showMenu(View v){
PopupMenu popup = new PopupMenu(context, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.your_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item1:
//your code
return true;
case R.id.menu_item2:
//your code
return true;
case R.id.menu_item3:
//your code
return true;
default:
return false;
}
}
});
popup.show();
}
Call this method in onClickListener of your button and pass your button.
The only way I can think of is to use an onClickListener() as part of the activity:
public class MyActivity extends Activity implements OnClickListener{
protected void onCreate(Bundle bundle) {
//Usual Activity Stuff
View v = (View)findViewById(R.id.view);
v.setOnClickListener(this);
}
public void onClick(View v) {
super.onClick(v);
this.openContextMenu(v);
}
}
Instead of creating a new View specifically for this, I guess you would use whatever View you wanted this to apply to. I hope this is what you were going for and that this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With