Search code examples
javaandroidlistviewonlongclicklistener

OnLong Click ListView Pop-UP


I would like to implement in my listview the longclick, up there I succeeded perfectly :) Now I would like to create a sort of pop-up with more possibilities, for example, paste, delete, remove, etc ... To make you understand I found a picture on the internet :) Ve mail below. Thank you in advance

enter image description here


Solution

  • Try to use pop menu like this..

    public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button2);
        registerForContextMenu(button);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.main, menu);
    }
    
    public void showPopup(View v) {
        PopupMenu popup = new PopupMenu(this, v);
        getMenuInflater().inflate(R.menu.main, popup.getMenu());
        popup.show();
        popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    
            @Override
            public boolean onMenuItemClick(MenuItem arg0) {
                Toast.makeText(MainActivity.this, "" + arg0, Toast.LENGTH_SHORT)
                        .show();
                return false;
            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    

    }

    And xml like this..

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item
        android:id="@+id/edit"
        android:title="edit"/>
    <item
        android:id="@+id/delete"
        android:title="delete"/>
    <item
        android:id="@+id/exit"
        android:title="exit"/>