GondraKkal
GondraKkal

Reputation: 97

Android: Menù popup with long click?

I need to implement a menù that appears with a long click around a button, so the user can choose with option he wants to take simply sliding toward a certain direction. Is there any way to do that? I have just a setOnLongClickListener with a onLongClick method for now.

btn01.setOnLongClickListener(new View.OnLongClickListener(){
            public boolean onLongClick (View view) {
                Toast.makeText(getApplicationContext(),"Button 01 long clicked", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

Upvotes: 1

Views: 3027

Answers (1)

Dany Pop
Dany Pop

Reputation: 3648

In your Activity :

    btn01.setOnLongClickListener(new View.OnLongClickListener(){
        public boolean onLongClick (View view) {
            registerForContextMenu(btn01);
            openContextMenu(btn01);
            return true;
        }
    });

    @Override
    public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
        //Context menu
        menu.setHeaderTitle("My Context Menu");
        menu.add(Menu.NONE, CONTEXT_MENU_VIEW, Menu.NONE, "Add");
        menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Edit");
        menu.add(Menu.NONE, CONTEXT_MENU_ARCHIVE, Menu.NONE, "Delete");
    }

    @Override
    public boolean onContextItemSelected (MenuItem item){
        switch (item.getItemId()) {
            case CONTEXT_MENU_VIEW: {

            }
            break;
            case CONTEXT_MENU_EDIT: {
                // Edit Action

            }
    }
}

Upvotes: 2

Related Questions