How to open the ContextMenu from MenuItem Android - android

So, I know we need to pass a view to openContextMenu(view); but where can i get the view for Menuitem, please have a look at my code. Thanks in advance.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionmenu:
openContextMenu(item); //I dont know what to pass here
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, 0, 11, "Edit");
menu.add(0, 1, 12, "Delete");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
System.out.println(item.getItemId());
return super.onContextItemSelected(item);
}

1. Register a view for a floating context menu
By default, a long-press on a view does not trigger the creation of a context menu.
You must register a view for a floating context menu by calling the following method,
a listview for example:
ListView listView = (ListView) v.findViewById(android.R.id.list);
registerForContextMenu(listView);
2. Create resource xml file
You'd better to create a xml resource file that contains the context menu item:
your_context.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/context_menu_eidt"
android:title="Edit" />
<item android:id="#+id/context_menu_delete"
android:title="Delete" />
</menu>
3. Inflate the resouce to build context menu
Then inflate the resource file in onCreateContextMenu method,
the parameter #v is the view that the context menu is being built for:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
if (v.getId() == android.R.id.list) {
Log.d(TAG, "get the view here");
}
getActivity().getMenuInflater().inflate(R.menu.your_context, menu);
}
4. Do something when menu item selected
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int position = info.position;
switch (item.getItemId()) {
case R.id.context_menu_eidt:
// TODO
return true;
case R.id.context_menu_delete:
// TODO
return true;
}
return super.onContextItemSelected(item);
}
That's all.

Related

Why doesn't the context menu appear in the center of the screen?

I read that the context menu should appear centered automatically, but my context menu changes positions based on where I click. This is what happens when I click on the first list item (menu in the lower right corner), and this is what happens for the other items.
Here is my code:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle("E adesso?");
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
mvc.controller.setHDImage(getContext(), info.position);
}
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.same_author_images:
return true;
case R.id.share:
shareImage(item);
return true;
default:
return super.onContextItemSelected(item);
}
}
context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/same_author_images"
android:title="Immagini stesso autore" />
<item
android:id="#+id/share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:title="Condividi" />
</menu>

ListView - onContextItemSelected manipulate the item instead of toString

As you can see in the code below, when I LongClick on an item in ListView, i get a popup menu with option to manipulate the item (delete, update, etc).
the problem is that I use my function on item.toString instead the item itself.
How can i get the item itself, and put it as an argument in my functions?
onCreateContextMenu:
public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.db_list_view) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
}
onContextItemSelected:
public boolean onContextItemSelected(android.view.MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
Object obj = lv.getItemAtPosition(info.position);
String nameToString = obj.toString();
if (item.getTitle().equals("Delete")) {
deletePlayerFromLongClick(nameToString);
} else if (item.getTitle().equals("Update")) {
updatePlayerFromLongClick(nameToString);
} else if (item.getTitle().equals("Change Host/Guest")) {
changeMembership(nameToString);
}
return true;
}
do like:
yourAdapter.getItem(info.position);
or
((YourAdapter)lv.getAdapter()).getItem(position);
or even simpler,
listOfItem.get(info.position);

Get position of the item LongClicked in a ListView

I have a ListView displaying custom views (3 TextViews per item); I have just implemented a context menu like such:
// in onCreateView
ListView list = (ListView) view.findViewById(R.id.list);
registerForContextMenu(list);
and
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.list) {
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.todo_context_menu, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
// your first action code
return true;
case R.id.delete:
// your second action code
return true;
default:
return super.onContextItemSelected(item);
}
}
I wish to know which item has been long clicked (its position in the ListView would be ideal) in order to edit or delete the right one.
How may that be achieved?
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
switch (item.getItemId()) {
case R.id.edit:
// your first action code
return true;
case R.id.delete:
// your second action code
return true;
default:
return super.onContextItemSelected(item);
}
}

I want show a menu when longClick

i have 1 app in the play store and i want do a new menu for him.
When a item of a list view is press i want show a menu like a whatsapp.
i have this.
What I have to do to display a menu like the whatsapp?
lista is a listview
lista.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
registerForContextMenu(lista);
return true;
}
});
now i use contextmenu, but when i click long in item the contextmenu no works
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Menú");
menu.add(0, v.getId(), 0, "Abrir");
menu.add(0, v.getId(), 0, "Cambiar nombre");
menu.add(0, v.getId(), 0, "Borrar");
menu.add(0, v.getId(), 0, "Poner alarma");
}
I believe what you're looking for is ContextMenu.
Reference
Guide
1º declare the items for a context menu in
/res/menu/NAMEOFXMLUWANT.XML
like this
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/Abrir"
android:title="#string/Abrir" />
<item android:id="#+id/Editar"
android:title="#string/Edit" />
<item android:id="#+id/Borrar"
android:title="#string/delete" />
<item android:id="#+id/Alarma"
android:title="#string/Alarma" />
</menu>
2º-AFTER in the class of u want do the context menu in LIST VIEW
lista = me listview
lista = (ListView) findViewById(R.id.Lista);
final ArrayAdapter<String> adaptador = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, ficheros);
lista.setLongClickable(true);
lista.setAdapter(adaptador);
lista.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) { //here u set u rute
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
}
});
3º AND FINALY, u set u onclick to items
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.Editar:
System.out.println("Editar");
return true;
case R.id.Borrar:
System.out.println("borrar");
return true;
case R.id.Abrir:
System.out.println("Abrir");
return true;
case R.id.Alarma:
System.out.println("Alarma");
return true;
default:
return super.onContextItemSelected(item);
}
}
for me all this WORKS :)

What's the difference between ContextMenuInfo.id and item.getItemId in the callback onContextItemSelected()?

What's the difference between ContextMenuInfo.id and item.getItemId in the callback onContextItemSelected()?
How can I display a ContextMenu in my AVD? Thank you!!
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
Inflate the menu before passing it into super.onCreateContextMenu.
ContextMenuInfo is the extra data that the view that initiated the context menu can setup - so there are edit and delete buttons with different item.getItemId(), but info.id gives the view that was used to create the menu (the item to be edited).

Categories

Resources