Add delete function in my project - android

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

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

Context Menu not being displayed

Here is the section of my code in onCreate() Method:
LayoutX = (LinearLayout) findViewById(R.id.LL_SomeName);
LayoutX.setClickable(true);
registerForContextMenu(LayoutX);
Here is rest of the code
CreateContextMenu
#Override
public void onCreateContextMenu(ContextMenu M, View V, ContextMenuInfo CMI) {
super.onCreateContextMenu(M, V, CMI);
M.setHeaderTitle("My Title Here");
M.add(0, V.getId(), 0, "Menu 1");
M.add(0, V.getId(), 0, "Menu 2");
}
ItemSelected
#Override
public boolean onContextItemSelected(MenuItem Item) {
Toast.makeText(getApplicationContext(), Item.getTitle(), Toast.LENGTH_LONG).show();
}
The Context Menu does not appear at all. Am I missing anything ?
this may help you
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select one");
menu.add(0, 1, 0, "Edit");
menu.add(0, 2, 0, "Delete");
}
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1){
// edit option is selected
}

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

I need help making a longpress Context menu on android

Ok, so now after reading I do actually have a menu on long press like I wanted...the only problem is that it doesn't actually get the sound file and save it
I am wondering what did I do wrong now? Here is the code I used:
Button SoundButton1 = (Button) findViewById(R.id.money);
registerForContextMenu(SoundButton1);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, MENU_RINGTONE, 0, "Ringtone");
menu.add(0, MENU_NOTIFICATION, 0, "Notification");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
Toast.makeText(this, "Ringtone saved", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
Toast.makeText(this, "Notification saved", Toast.LENGTH_SHORT).show();
}
You are not comparing Strings properly. This test:
if(item.getTitle()=="Ringtone")
checks if the object returned by getTitle() is the same object as "Ringtone". This will always be false. Similarly with the else if test. You actually want to compare the value of getTitle with your String. You should replace them with the String#equals() method. Here is the code:
if(item.getTitle().equals("Ringtone")){function1(item.getItemId());}
else if(item.getTitle().equals("Notification")){function2(item.getItemId());}
else {return false;}
If you are using a ListFragment then you also need to do
getListView().setOnCreateContextMenuListener(this);
instead of
registerForContextMenu(listView);
it seems.

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