Android: ContextMenu and ItemSelected in Context Menu - android

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

Related

replace of menu item position by another menu item position in same menu

I am trying to create a context menu for card view.Initial menu like
fig 1: initial context menu
and i need to replace it like fig 2: replaced context menu
.When i click on disable menu the card view will be disabled,and the disable menu should replace with enable menu
call invalidateOptionsMenu() after click on menu item to change menu item title.
Boolean IsEnable = false;
#Override
public boolean onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuItem reminderstatus = (MenuItem) menu.findItem(R.id.reminderstatus);
if (IsEnable) {
reminderstatus.setTitle("Disable");
} else {
reminderstatus.setTitle("Enable");
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
if (item.getItemId() == R.id.reminderstatus) {
if (IsEnable) {
IsEnable = false;
} else {
IsEnable = true;
}
invalidateOptionsMenu();
}
}
Actually you don't need to create Different Menu item for Enable and Disable , take a single menu-item , just change the text of menu item , from their Status (Enable or Disable).
hope you are saving status that currently it is Enable or Disable.
#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);
// Select a menu item then change it's title (text)
MenuItem mi = (MenuItem) menu.findItem(R.id.YOUR_MENU_ID);
if(CHECK_YOUE_CURRENT_STATUS_HERE){
//SET YOUR CURRENT STATUS ACCORDINGLY CURRENT STATUS (ENABLE /DISABLE)
mi.setTitle("Enable/Disable");
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
if (item.getItemId() == R.id.reminderstatus) {
if (YOUR_CURRENT_STATUS) {
YOUR_CURRENT_STATUS = false;
} else {
YOUR_CURRENT_STATUS = true;
}
invalidateOptionsMenu();
//this method refresh your Context menu view so basically call
// your onCreateContextMenu once again which will check for your
// Status and set accordingly it.
}
}
Just check this sample code implement it correctly will work for you.

Android Context Menu for any View

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);
}
}

Enabling Webview To save images on Long Tap

i am trying to make my webview bring an option to save the current image when the image is long tapped i have tried the below code outside the oncreate but it does not do anything at all!
public boolean onLongClick(View v) {
openContextMenu(v);
return true;
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.save_image:
Toast.makeText(this, "save failed",
Toast.LENGTH_LONG).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
#Override
protected void onCreateContextMenu(ContextMenu menu) {
super.onCreateContextMenu(menu);
HitTestResult result = getHitTestResult();
MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// do the menu action
return true;
}
};
if (result.getType() == HitTestResult.IMAGE_TYPE ||
result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
// Menu options for an image.
//set the header title to the image url
menu.setHeaderTitle(result.getExtra());
menu.add(0, ID_SAVEIMAGE, 0, "Save Image").setOnMenuItemClickListener(handler);
menu.add(0, ID_VIEWIMAGE, 0, "View Image").setOnMenuItemClickListener(handler);
}
}

onContextItemSelected Error

I having some difficulty implementing a context menu into my android application. My first problem was I was trying to implement OnCreateContextMenu inside of OnCreate but I kept getting an error saying:
void is an invalid type for the variable onCreateContextMenu
I fixed this problem by putting onCreateContextMenu outside of OnCreate. Now my problem lies with OnContextItemSelected. My error occurs on the line: public boolean onContextItemSelected(MenuItem menu). The errors are:
implements android.view.View.OnLongClickListener.onLongClick
Syntax error, insert "}" to complete MethodBody
Here is the code:
BaconStripsButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
boolean onContextItemSelected(MenuItem item)
{
if (item.itemId() = 0)
{
Toast ringtone = Toast.makeText(startingPoint.this, "Ringtone added Successfully!", Toast.LENGTH_SHORT);
return true;
}
return false;
}
}
});
Any help would be appreciated. Thanks, Justin
No need to use onContextItemSelected Inside onlongClick of button.Just Override OnContextItemSelected(); and register ContextMenu to btn.No need to setOnlongClickListener.
#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);
}
Then override
#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);
}
}
then finally registerContextMenu(button);
You may use the ListView and implement the onCreateContextMenu in the OnCreate of the Activity.
like this:
myList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
// add some sublist
menu.setHeaderTitle(R.string.collect_title);
menu.add(0, 1, 0, R.string.delete_string);
menu.add(0, 2, 0, R.string.move_to_project_string);
menu.add(0, 3, 0, R.string.move_to_action_string);
}
});

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