Context menu with list view
In Java you must compare strings with .equals() and not ==.
Also, it looks like you are inflating a ContextMenu. You need to implement onContextItemSelected(MenuItem item) and not onOptionsItemSelected(MenuItem).
Take a look here: https://developer.android.com/guide/topics/ui/menus.html and scroll down to create a floating context menu.
How to show ContextMenu long click on listview android?
Remove longpress method from your code just, hope it will working
android get listview item by contextmenu
Since your Adapter's data list consists of StructReceiveSms
objects, the adapter.getItem(info.position)
call in onContextItemSelected()
will return the list item the context menu was opened for, but it will need to be cast to the StructReceiveSms
type. From this, you can get the userID
and username
you want.
public boolean onContextItemSelected(MenuItem item)
{
...
StructReceiveSms listItem = (StructReceiveSms) adapter.getItem(info.position);
String selectedName = listItem.username;
int selectedId = listItem.userID;
...
}
This is assuming you've not overridden the getItem()
method of the Adapter to return something else, but I imagine you would've shown that if you had.
why item context menu is not showing up in listview
Context menu work on long press of list item rather than short click. If you want it to work on short click implement onListItemClick
method as below
public void onListItemClick(ListView l, View v, int position, long id){
list.showContextMenuForChild(v);
}
If you want to open the same on some other element like a button you can do as -
public void onButtonClickEvent(View sender)
{
registerForContextMenu(sender);
openContextMenu(sender);
unregisterForContextMenu(sender);
}
Otherwise you can also use PopupMenu
add in layout in listview item as -
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_overflow_holo_dark"
android:contentDescription="@string/descr_overflow_button"
android:onClick="showPopup" />
Then implement your showPopup method as below -
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.actions, popup.getMenu());
popup.show();
}
Context menu android on Listview item with CustomAdapter doesn't show
You've set an OnItemLongClickListener
for the ListView
, which will run before the context Menu is created. Returning true
from its onItemLongClick()
method indicates that the long click is being consumed there, so the Menu will not be created.
If you don't actually need the OnItemLongClickListener
, you can remove the relevant code. If you do need it as well, for some reason, you can return false
from onItemLongClick()
, and your Menu will then show.
Android: help create Context menu from listview
This should do the trick. Don't forget to: registerForContextMenu(lvUsers);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select);
mListUsers = getUsers();
lvUsers = (ListView) findViewById(R.id.lv_user);
lvUsers.setAdapter(new ListAdapter(this, R.id.lv_user, mListUsers));
**registerForContextMenu(lvUsers);**
}
@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);
}
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/edit"
android:title="@string/edit" />
<item android:id="@+id/delete"
android:title="@string/delete" />
</menu>
Android ListView with context menu
Just Create menu.xml in res/menu folder Like below example
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_item1" android:title="@string/menu_item1"/ >
<item android:id="@+id/menu_item2" android:title="@string/menu_item2" >
<!-- "file" submenu -->
<menu>
<item android:id="@+id/sub_menu_item1"
android:title="@string/sub_menu_item1" />
<item android:id="@+id/sub_menu_item2"
android:title="@string/sub_menu_item2" />
</menu>
Create an image Button like bellow example in your layout
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_overflow_holo_dark"
android:contentDescription="@string/descr_overflow_button"
android:onClick="showPopup" />
Create Method that display your popup-menu.
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
// action is your menu.xml file
// This activity implements OnMenuItemClickListener
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
// your menu id and perform action
case R.id.archive:
archive(item);
return true;
case R.id.delete:
delete(item);
return true;
default:
return false;
}
}
and you can follow This tutorial
Related Topics
How to Retrieve the Android Sdk Version
Add Ripple Effect to My Button with Button Background Color
Install_Failed_Update_Incompatible When I Try to Install Compiled .Apk on Device
Firebase (Fcm): Open Activity and Pass Data on Notification Click. Android
Android - Nullpointerexception on Searchview in Action Bar
Android Eclipse Ddms - Can't Access Data/Data/ on Phone to Pull Files
Securityexception: Caller Uid Xxxx Is Different Than the Authenticator's Uid
How to Know When an Edittext Loses Focus
Upload Photo to Facebook with Facebook Android Sdk in Android
How to Send and Receive Data Sms Messages
Local Image Caching Solution for Android: Square Picasso, Universal Image Loader, Glide, Fresco
How to Display Custom View in Actionbar
How to Save State During Orientation Change in Android If the State Is Made of My Classes
Android Studio Error Installing Gradle
How to Split Comma-Separated Value in SQLite
Continue Service Even If Application Is Cleared from Recent App