user3051460
user3051460

Reputation: 1485

Is it possible to show context menu at position of item in listview?

I have a listview. I want to click a item and show a context menu at click item position. Currently, I am using the below code to show context menu and perform some action when I click the item. It works well but the context menu position is automatic setting at center of my screen. How can I fix it?

public class ManageActivity extends Activity {
  private ListView mainListView ;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_main);    
    // Find the ListView resource. 
    mainListView = (ListView) findViewById( R.id.mainListView );
    mainListView.setAdapter( listAdapter );     
    registerForContextMenu(mainListView);
 }
  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
      menu.add(0, v.getId(), 0, "Edit");
      menu.add(0, v.getId(), 0, "Delete");
  }
  @Override
  public boolean onContextItemSelected(MenuItem item){
     AdapterView.AdapterContextMenuInfo acm = (AdapterContextMenuInfo) item.getMenuInfo();
     String  audio_file_name = (String) mainListView.getItemAtPosition(acm.position);

     if(item.getTitle()=="Edit")
        Toast.makeText(getApplicationContext(), "Edit Clicked", Toast.LENGTH_LONG).show();
     if(item.getTitle()=="Delete")
          Toast.makeText(getApplicationContext(), "Delete Clicked", Toast.LENGTH_LONG).show();
    return true;
}

Upvotes: 2

Views: 3279

Answers (2)

Elltz
Elltz

Reputation: 10859

ListView.setonItemLongClickListener(); then you anchor your popumenu to it

Upvotes: 1

Iliiaz Akhmedov
Iliiaz Akhmedov

Reputation: 877

The answer is yes. A perfect example is gridview items in Google Play App. Basically, you can find more details here:

Android - Popup menu when list item view pressed?

Upvotes: 3

Related Questions