Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Context Menu With Short Click not Long Click

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.

like image 798
seba123neo Avatar asked Dec 29 '10 05:12

seba123neo


People also ask

Is shortcut menu a context menu?

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.

How do I display context menu?

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.

What is contextual click?

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.

What are context menu commands?

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.


2 Answers

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.

like image 28
Šimon Vyhnis Avatar answered Nov 05 '22 13:11

Šimon Vyhnis


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.

like image 166
Matt Avatar answered Nov 05 '22 12:11

Matt