Android Context Menu for any View - android

As far as I knew Context Menu can be registered for any view not only for ListView. Today I am trying to register context menu for an Custom ImageView. Like this:
// In onCreateView() method of activity
registerForContextMenu(mViewHolder.profileImageView);
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
switch (v.getId()) {
case R.id.profile_imageview:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.add(Menu.NONE, CONTEXT_MENU_ITEM_CHANGE_PICTURE, 0, "Change Picture");
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
switch (item.getItemId()) {
case CONTEXT_MENU_ITEM_CHANGE_PICTURE:
//Toast.makeText(mParentActivity, "Delete " + info.position + "th item", Toast.LENGTH_LONG).show()
Toast.makeText(this, "Go to library", Toast.LENGTH_SHORT).show();
return true;
default:
return true;
}
}
I didn't find any example of registering context menu without ListView. Would anyone give me one example or can figure out what I am missing ?

you need to write super.onCreateContextMenu(menu, v, menuInfo);
after all code like:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
switch (v.getId()) {
case R.id.profile_imageview:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.add(Menu.NONE, CONTEXT_MENU_ITEM_CHANGE_PICTURE, 0, "Change Picture");
super.onCreateContextMenu(menu, v, menuInfo);
}
}

Related

Detecting onContextMenu clicks in a fragment

So I've successfully connected a context menu pop up to a list view in a fragment. The items show up, but when I click on them, onContextMenuItemSelectedMenu() is ignored and instead onMenuItemClick() is called in the parent activity. How can I make it so when I click the context menu items onContextMenuItemSelectedMenu() is called in the fragment instead. Thanks.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("item1");
menu.add("item2");
menu.add("item3");
}
#Override
public boolean onContextItemSelected (android.view.MenuItem item){
Log.i("cTest", "clicked context menu");
return true;
}
I figured it out. It's turned out to be the same as buttons. Both in the fragment:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("item0").setOnMenuItemClickListener(this);
menu.add("item1").setOnMenuItemClickListener(this);
}
#Override
public boolean onMenuItemClick(MenuItem item){
if(itemName.equals("item0))
{
}
else if (itemName.equals("item1"))
{
}
}

Android: ContextMenu and ItemSelected in Context Menu

This is the onCreate and oncontextitemslected code
#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) {
Toast toast;
if(item.getItemId() == R.id.context_menu_edit)
{
Log.d("ContextCheck","EDIT!");
toast = Toast.makeText(this, "Edit!", Toast.LENGTH_SHORT);
toast.show();
}
if(item.getItemId() == R.id.context_menu_delete)
{
Log.d("ContextCheck","DELETE!");
toast = Toast.makeText(this, "Delete!", Toast.LENGTH_SHORT);
toast.show();
}
return super.onContextItemSelected(item);
}
and before that is i used the method registerForContextMenu(event_list) where event_list is a ListView , no i don't know why when ever i click an item from the context menu, it doesn't do anything, it won't show the toast and won't log into the logcat... is the item.getItemId() same for OptionsMenu and ContextManu?.. i don't know what is wrong with my code..
PS
the context menu is called inside a dialog box in a listview
Here is your solution, if you don't mind creating the menu items in your class. The keyword was definitely your PS, meaning your listview is in a dialog.
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//MenuInflater inflater = getMenuInflater();
//inflater.inflate(R.menu.context_menu, menu);
MenuItem delete = menu.add("delete");
MenuItem add = menu.add("add");
add.setIcon(android.R.drawable.ic_menu_upload); //adding icons
delete.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Log.d("ContextCheck","EDIT!");
Toast.makeText(Pr.this, "Edit!", Toast.LENGTH_SHORT).show();
return true;
}
});
add.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Log.d("ContextCheck","EDIT!");
Toast.makeText(Pr.this, "Edit!", Toast.LENGTH_SHORT).show();
return true;
}
});
}
You do not even need the onContextItemSelected method.
You need to return true in the onCreateOptionsMenu as detailed in the documentation:
Returns
You must return true for the menu to be displayed; if you
return false it will not be shown.
So you can chacnge your code to this:
#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);
return true;
}
UPDATE:
I have things return on the options menu with a switch case in a onOptionsItemSelected vs onContextItemSelected
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.emaildev:
email();
break;
case R.id.share:
Share();
break;
}
return true;
}
The icons and ids are in my menu.xml

Android: Context Menu to Alert dialog

What do i need to change to make this display into alert dialog?
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteNote(info.id);
fillData();
return true;
}
return super.onContextItemSelected(item);
}
I'm a beginner in android so i still don't know how to implements some things. I want this to appear as alert dialog. Currently the user needs to longkeypress to activate the delete. However i want to prompt it as an alert dialog and have choices for the user to choose from if he wants to really delete it or not.
In your case DELETE_ID do this:
new AlertDialog.Builder(this).setTitle("Confirm Delete")
.setMessage("Do you want to delete this blank?")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteNote(info.id);
fillData();
}
})
.setNeutralButton("Cancel", null) // don't need to do anything but dismiss here
.create()
.show();
You need to put the delete logic in the OK click listener.
You only need to implement the following function. It will work.
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
Log.e(LOGTAG, "Tao menu");
if(v == expList)
{
super.onCreateContextMenu(menu, v, menuInfo);
//AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;
// We know that each row in the adapter is a Map
//HashMap map = (HashMap) simpleAdpt.getItem(aInfo.position);
menu.setHeaderTitle("Options");
menu.add(1, 1, 1, "Reprint");
menu.add(1, 2, 1, "Void");
menu.getItem(0).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{
return true;
}
});
menu.getItem(1).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{
return true;
}
});
}
}

Invoke android context menu on menu item press

Can anyone kindly guide as to how can I invoke a context menu on the press of a menu item. I googled a lot for the same, but nothing turned up.
Look forward for your valuable help.
Regards,
Rony
You are probably looking for openContextMenu(view). Call it in your Menu's onclick()
To create a context menu, override onCreateContextMenu and onContextItemSelected. Refer google for examples.
You only need to implement this function. It will work.
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
Log.e(LOGTAG, "Tao menu");
if(v == expList)
{
super.onCreateContextMenu(menu, v, menuInfo);
//AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;
// We know that each row in the adapter is a Map
//HashMap map = (HashMap) simpleAdpt.getItem(aInfo.position);
menu.setHeaderTitle("Options");
menu.add(1, 1, 1, "Reprint");
menu.add(1, 2, 1, "Void");
menu.getItem(0).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{
return true;
}
});
menu.getItem(1).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem clickedItem)
{
return true;
}
});
}
}

How to remove an array item from a context menu?

I have a ListView and would like to remove a row item when the user long clicks on selects Remove from the context menu.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Selection Options");
menu.add(0, v.getId(), 0, "Remove Symbol");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Remove Symbol"){
Toast.makeText(this, "Remove clicked!", Toast.LENGTH_SHORT).show();
}
else {
return false;
}
return true;
}
How can I get a reference to the row number that was clicked, so I can remove that index from my array?
In your onContextItemSelected callback, you can use this code to get the id of the item.
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
removeItemFromListById(info.id);
}
Source:
Creating Menus | Android Developers

Categories

Resources