How can I tell which button has accessed the context menu - android

In my soundboard app I have created a context menu using this code.
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Choose an option");
menu.add(0, v.getId(), 0, "Save as ringtone");
menu.add(0, v.getId(), 0, "Save as Notification");
menu.add(0, v.getId(), 0, "Save as Alarm");
menu.add(0, v.getId(), 0, "Exit Menu");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
// Global.currentsound = info.id;
if(item.getTitle()=="Save as ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Save as Notification"){function2(item.getItemId());}
else if(item.getTitle()=="Save as Alarm"){function3(item.getItemId());}
else {return false;}
return true;
}
and called on the buttons like this
Button cmenu = (Button)findViewById(R.id.s1sound1);
registerForContextMenu(cmenu);
Now I want to pass the information for each button to the function in the code to set the sound according to which button was pressed. How would I do this without creating a separate context menu for each button which would be madness.
Thanks

I think it is stored in the "View v", Button extends View, so you have to cast it. I'm not sure, but you can check by setting a breakpoint on the
super.onCreateContextMenu(menu, v, menuInfo);
line, and checking the debugger.

Related

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

Is it possible to have 2 context menus in same activity?

I have an activity with TextView and an image icon. I already created context menu for the image icon by overriding onCreateContextMenu().
However, I would like also to have another context menu for the TextView. However, I notice I'm already utilizing onCreateContextMenu():
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(getString(R.string.option1));
menu.add(0, v.getId(), 0, getString(R.string.option2));
menu.add(0, v.getId(), 0, getString(R.string.options3));
}
Hence, is there away to add another context menu for the TextView?
you can use the passed View object in onCreateContextMenu to determine the owner of the menu and populate a menu accordingly.
Your code should look something like this :
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
switch (v.getId()) {
case R.id.imageIconId:
menu.setHeaderTitle(getString(R.string.option1));
menu.add(0, v.getId(), 0, getString(R.string.option2));
menu.add(0, v.getId(), 0, getString(R.string.options3));
break;
case R.id.textViewid:
// do whatever you want with the menu object.
break;
}
}
The parameters passed to you by that method will help you branch to different things.
Here is an example:
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if( v.equals(yourImgView) ){
menu.setHeaderTitle(getString(R.string.option1));
menu.add(0, v.getId(), 0, getString(R.string.option2));
menu.add(0, v.getId(), 0, getString(R.string.options3));
}else if ( v.equals(yourTxtView) ) {
//Do your textView things.
}
}

Add delete function in my project

I use this code for my contextual menu and I make a simple commands. But how to integrate real commands in this?
I want make real function.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Optiuni");
menu.add(0, v.getId(), 0, "Copiaza");
menu.add(0, v.getId(), 0, "Sterge");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Copiaza"){copyFunction(item.getItemId());}
else if(item.getTitle()=="Sterge"){deleteFunction(item.getItemId());}
else {return false;}
return true;
}
public void deleteFunction(int id){
Toast.makeText(this, "Sters", Toast.LENGTH_SHORT).show();
}
public void copyFunction(int id){
Toast.makeText(this, "Copiat", Toast.LENGTH_SHORT).show();
}

getItemId returns first textview id in list rather than the clicked one

I have a list of dynamically created TextViews for which I am creating context menu. However my code returns id of first text view rather than the one clicked one.
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// Create your context menu here
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Edit n Replace");
menu.add(0, v.getId(), 1, "Delete");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
// Call your function to preform for buttons pressed in a context menu
// can use item.getTitle() or similar to find out button pressed
// item.getItemID() will return the v.getID() that we passed before
super.onContextItemSelected(item);
if ( item.getTitle().toString().equals("Delete")){
NotesDatabase db =new NotesDatabase(this);
TextView tv = (TextView) findViewById(item.getItemId());
db.searchAndDelete(tv.getText().toString());
}
return true;
}
you add same ID for both TextView
menu.add(0, v.getId(), 0, "Edit n Replace");
menu.add(0, v.getId(), 1, "Delete");
try with different id for each Text View. like this :
menu.add(0, 1, 0, "Edit n Replace");
menu.add(0, 2, 1, "Delete");
You should need to listen List Action onClick(View v) Event see Example.
//Declare String Global Variable
String result=null;
//Get TextView when onClick before click contextmenu
public void onClick(View v)
{
TextView tv = (TextView)v.findViewById(R.id.yourTextViewId);
result=tv.getText().toString();
}
public boolean onContextItemSelected(MenuItem item)
{
super.onContextItemSelected(item);
if ( item.getTitle().toString().equals("Delete")){
NotesDatabase db =new NotesDatabase(this);
TextView tv = (TextView) findViewById(item.getItemId());
db.searchAndDelete(result);
}
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