Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android opening context menu after button click

Tags:

I want to open context menu when I click a button, but also I have to know which list item is focused when I click the button. Do you know how to do that? What code should be in onclick method?

like image 440
Mustafa İrer Avatar asked Apr 13 '11 06:04

Mustafa İrer


People also ask

How to show context menu on button click in android?

Android context menu appears when user press long click on the element. It is also known as floating menu. It affects the selected content while doing action on it. It doesn't support item shortcuts and icons.

What is contextual menu in android?

A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

Which method is used to register view for context menu?

In Activity class, there is method called registerForContextMenu(View view) . The android document explains that this method is used to registers a context menu to be shown for the given view (multiple views can show the context menu).


1 Answers

I was looking for the same, and found that instead of context menu, you should use Dialogs

final CharSequence[] items = {"Red", "Green", "Blue"};  AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Pick a color"); builder.setItems(items, new DialogInterface.OnClickListener() {     public void onClick(DialogInterface dialog, int item) {         Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();     } }); AlertDialog alert = builder.create(); alert.show(); 

http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog

like image 71
dikirill Avatar answered Jan 03 '23 00:01

dikirill