AngryHacker
AngryHacker

Reputation: 61606

How to implement a popup on Android?

I've seen a bunch of application where holding down an entry on an ListView for a bit (longer than a click) produces a popup. It typically lists the actions to take on the entry (edit, delete, etc...).

Is this something that's built into Android or something I have to build on my own?

Upvotes: 0

Views: 481

Answers (4)

keyser
keyser

Reputation: 19189

Like the previous poster said you can create a Dialog, and that long click you mentioned can be implemented using a setOnItemLongClickListener. Good luck!

(Edited listener from longclick to itemlongclick)

Upvotes: 1

MH.
MH.

Reputation: 45493

Above suggestions don't look like the most straightforward approach to me. Just as a ListView has a setOnItemSelectedListener, there is an equivalent for long clicks, called setOnItemLongClickListener.

If you combine this listener with onContextItemSelected (as illustrated by Noureddine AMRI) for your actual context menu, you've got everything you need. Implementation examples are widespread.

Upvotes: 1

Noureddine AMRI
Noureddine AMRI

Reputation: 2942

Holding an entry a bit longer will trigger a context menu.

Use this in onCreate:

registerForContextMenu(getListView());

then override:

@Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                .getMenuInfo();
        int idcompte = mComptes.get(info.position).getId();

        switch (item.getItemId()) {
            case DELETE_ID:
                DBhelper dBhelper = new DBhelper(this);
                dBhelper.open();
                dBhelper.deleteCompte(idcompte);
                dBhelper.close();
                onResume();
                return true;

            case EDIT_ID:

                Intent intent = new Intent(this, AddorupdateCompteActivity.class);
                intent.putExtra(AddorupdateCompteActivity.ID, idcompte);
                startActivity(intent);

                return true;
        }
        return super.onContextItemSelected(item);
    }

Upvotes: 1

zmbq
zmbq

Reputation: 39013

You can create a Dialog (there's a lot of documentation about Android dialogs, you can start here http://developer.android.com/guide/topics/ui/dialogs.html ). Or, you can start another activity, which to me seems more Android-friendly, although it might just be a feeling.

Upvotes: 2

Related Questions