get view id from oncontextitemselected - android

I've several buttons registered for context menu
how do I know which button was clicked for the menu to appear?
below is the pseudocode that i'll be using. I need to do something related to which button clicked (I have few more buttons to be declared), how do I know that the context menu is activated from which button click.
EDIT: I think i didn't make myself clear, I wanted to know which button was clicked for the menu to appear. Not which menu item is clicked. Anyways, I've a solution which I'll add in pretty soon.
thanks
private static final int SEND_AS_TEXT = Menu.FIRST;
private static final int SEND_AS_IMAGE = Menu.FIRST + 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendAllBtn = (Button)findViewById(R.id.sendAllBtn);
sendAllBtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
registerForContextMenu(v);
openContextMenu(v);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()){
case SEND_AS_TEXT:
//do sth related to the button clicked
break;
}
return super.onContextItemSelected(item);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(Menu.NONE, SEND_AS_TEXT, SEND_AS_TEXT, "Send As Text");
menu.add(Menu.NONE, SEND_AS_IMAGE, SEND_AS_IMAGE, "Send As Image");
}

Ok, thanks alot for the help from the others which clear my doubts on the getItemId since it returns the ID that I assigned to the menu item.
In my case, I wanted to know which button was clicked before the contextmenu was created.
To do this, I simply create a long variable to store the button that was clicked. The ID of the button can be obtained as in the following:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Send As..");
menu.add(Menu.NONE, SEND_AS_TEXT, SEND_AS_TEXT, "Send As Text");
menu.add(Menu.NONE, SEND_AS_IMAGE, SEND_AS_IMAGE, "Send As Image");
btnId = v.getId(); //this is where I get the id of my clicked button
}
and later on I'll only need to refer to this btnId to do whatever I want.

I think it makes more sense to use the ID of the specific view. Say you've got an ListView populated of items containing your data, but in-between some of the items you've created separators/headers. You don't want the separators to handle clicks/long clicks.
In some cases it's totally fine to just refer to "position" or MenuInfo.id, but depending on your data structure you might need more control.
What you can do is to set ID's for the items/views within your ListView (view.setId(x), where x represents the ID/position for your data structure/object. Then, when creating a ContextMenu and handling selections within it do the following to read that ID out:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
int id = info.targetView.getId();
// now you can refer to your data with the correct ID of yours
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int id = info.targetView.getId();
// now you can refer to your data with the correct ID of yours
}

If you are looking for the ID of your underlying data (provided by the adapter's getItemId(int)), then just add the following lines in the onContextItemSelected method:
final AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final long datasId = info.id // get datas id

#Override
public boolean onContextItemSelected(MenuItem item) {
item.getItemId();
return super.onContextItemSelected(item);
}

try this...
#Override
public boolean onContextItemSelected(MenuItem item)
{
if(item.getItemId()==SEND_AS_TEXT)
{
//code for send text
}
else if(item.getItemId()==SEND_AS_IMAGE)
{
//code for send image
}
return super.onContextItemSelected(item);
}

Related

Add one more option to android messages long press context menu

I wanted to write an app which can store some messages. Currently by doing long press on android messages some options (like "forward message", "delete message" etc) are arrived. I would like to add one more option to this (for example "store this message").
Is there a way to do this?
Try this code:
ListView nameList;
nameList = (ListView) findViewById(R.id.list);
nameList.setLongClickable(true);
registerForContextMenu(nameList);
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle("Delete / Miss Call Contact");
menu.add(menu.NONE,1,menu.NONE,"Delete");
menu.add(menu.NONE,2,menu.NONE,"call");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
position = (int)info.id;
switch(item.getItemId()) {
case DELETE:
// do something
case MISSCALL:
// do something
}
return super.onContextItemSelected(item);
}

RegisterForContextMenu ImageView?

I'm trying to use a floating context menu and I wonder if it's possible to activate this menu, by pressing the image in the ImageView?
My first problem is how to handle registerForContextMenu and the ImageView? I searched and find most examples with GridView and ListViews.
I have made the menu in xml and I should I use this method in the activity with a switch:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
}
Just like the others, you get your View in onCreateContextMenu, based on that you inflate the menu for the proper item.
registerForContextMenu(imageView);
The method above expects any View class.
Each time you call registerForContextMenu() for a different View, onCreateContextMenu() will be called for you to handle the proper menu creation.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId == R.id.youtImageView) {
getMenuInflater().inflate(R.menu.image_menu, menu);
}
}
Based on the item id you decide for which View the menu was clicked. You must me sure the id's of menu items for different views are not the same.
When the item from a context menu is clicked, you will receive the onContextItemSelected() callback with MenuItem that was clicked
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.image_menu_item_do_something:
doSOmething();
return true;
default:
return super.onContextItemSelected(item);
}
}
Make sure that you have these methods in onCreate:
ImageView image = (ImageView) findViewById(R.id.image_view);
registerForContextMenu(image);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openContextMenu(image);
}
});
And in context_menu_main.xml, that it looks similar to this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_option"
android:title="#string/action_option_text" />
</menu>
Finally, you'll need to override these two methods as follows:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.context_menu_main, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_option:
Log.e("TAG", "Option");
return true;
default:
return super.onContextItemSelected(item);
}
}

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

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