How to open Menu Context Android with click button in listview adapter ?
I tried with my code, but not show the menu context,
code
public View getView(int position, View convertView, ViewGroup parent) {
vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.tulisan_komentar_list_item,parent, false);
LinearLayout content_favorite= (LinearLayout)vi.findViewById(R.id.content_favorite);
final TextView date_komentar = (TextView)vi.findViewById(R.id.date_komentar); // artist name
final TextView isi_komentar = (TextView)vi.findViewById(R.id.isi_komentar); // duration
final TextView nama_komentar = (TextView)vi.findViewById(R.id.nama_komentar); // duration
final TextView id_tulisan_komentar = (TextView)vi.findViewById(R.id.id_tulisan_komentar); // duration
final ImageButton act_komentar = (ImageButton)vi.findViewById(R.id.act_komentar);
ImageView thumb_image=(ImageView)vi.findViewById(R.id.avatar_komentar); // thumb image
HashMap<String, String> tulisan = new HashMap<String, String>();
tulisan = data.get(position);
// Setting all values in listview
date_komentar.setText(tulisan.get(ContentCommentActivity.TAG_DATE_KOMENTAR));
isi_komentar.setText(tulisan.get(ContentCommentActivity.TAG_ISI_KOMENTAR));
nama_komentar.setText(tulisan.get(ContentCommentActivity.TAG_NAMA_KOMENTAR));
id_tulisan_komentar.setText(tulisan.get(ContentCommentActivity.TAG_ID_TULISAN_KOMENTAR));
String avatar_komentar = tulisan.get(ContentCommentActivity.TAG_AVATAR_KOMENTAR);
if(hide_gambar.equals("Y")){
thumb_image.setVisibility(View.GONE);
}
else{
thumb_image.setVisibility(View.GONE);
/* thumb_image.setVisibility(View.VISIBLE);
if (avatar_komentar.equals("")) {
thumb_image.setVisibility(View.GONE);
} else {
imageLoader.DisplayImage(tulisan.get(ContentCommentActivity.TAG_AVATAR_KOMENTAR), thumb_image);
thumb_image.setVisibility(View.VISIBLE);
} */
}
activity.registerForContextMenu(act_komentar);
act_komentar.setOnClickListener(new android.view.View.OnClickListener()
{
public void onClick(View v)
{
activity.openContextMenu(v);
v.showContextMenu();
}
});
return vi;
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.setHeaderTitle("My Context Menu");
menu.add(0, 1, 0, "Add");
menu.add(0, 2, 0, "Edit");
menu.add(0, 3, 1, "Delete");
}
can you tell me, how it should work ?
Use like this:
act_komentar.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View v) {
//To register the button with context menu.
registerForContextMenu(act_komentar);
openContextMenu(act_komentar);
}
});
final int CONTEXT_MENU_VIEW = 1;
final int CONTEXT_MENU_EDIT = 2;
final int CONTEXT_MENU_ARCHIVE = 3;
@Override
public void onCreateContextMenu (ContextMenu menu, View
v, ContextMenu.ContextMenuInfo menuInfo){
//Context menu
menu.setHeaderTitle("My Context Menu");
menu.add(Menu.NONE, CONTEXT_MENU_VIEW, Menu.NONE, "Add");
menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Edit");
menu.add(Menu.NONE, CONTEXT_MENU_ARCHIVE, Menu.NONE, "Delete");
}
@Override
public boolean onContextItemSelected (MenuItem item){
// TODO Auto-generated method stub
switch (item.getItemId()) {
case CONTEXT_MENU_VIEW: {
}
break;
case CONTEXT_MENU_EDIT: {
// Edit Action
}
break;
case CONTEXT_MENU_ARCHIVE: {
}
break;
}
return super.onContextItemSelected(item);
}
Output:
Hope this will work for you.