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);
}
Related
I have an app that lists items in a listview which is populated from Parse.com and when I add new items to listview it adds to Parse.com
That is working fine but I also have a popup context menu window that appears when you longpress the item.
I can get it to remove the item from the list but not to delete from Parse.com
Can anyone please help me?
This is my code for the context window;
`#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Options");
getMenuInflater().inflate(R.menu.action, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
String name = Name.getText().toString();
switch (item.getItemId()) {
case R.id.cnt_mnu_x2:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
break;
case R.id.cnt_mnu_custom:
Toast.makeText(this, "TEST SELECTED", Toast.LENGTH_SHORT).show();
break;
case R.id.cnt_mnu_delete:
adapter.remove(adapter.getItem(info.position));
Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
break;
}
return true;
}`
I have a list view connected to a database, showing a all the entries. I want a menu to show up if the user long clicks a item in the listview(database entry), showing options to edit or delete the entry. how can i do this.
Till now, I have tried using a onItemLongClick listener and a toast in it showing the id of the view long clicked.
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
String res = Long.toString(id);
Toast toast = Toast.makeText(this, res, Toast.LENGTH_SHORT);
toast.show();
return true;
}
First you need to register your context menu on list view.
lv = (ListView) findViewById(R.id.list_view);
registerForContextMenu(lv);
Then, just override activity methods.
/**
* MENU
*/
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.list_view) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.add:
// add stuff here
return true;
case R.id.edit:
// edit stuff here
return true;
case R.id.delete:
// remove stuff here
return true;
default:
return super.onContextItemSelected(item);
}
}
Here is an example of menu_list.xml file (you have to put the file in the res/menu folder).
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/add"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/menu_add" />
<item android:id="#+id/edit"
android:icon="#android:drawable/ic_menu_edit"
android:title="#string/menu_edit" />
<item android:id="#+id/delete"
android:icon="#android:drawable/my_icon_delete"
android:title="#string/menu_delete" />
</menu>
Hope it will help.
Instead of using onItemLongClick you can use
public void onCreateContextMenu(final ContextMenu menu,
final View v, final ContextMenuInfo menuInfo) {
...
}
where you setup the options for edit and delete or whatever you need to.
The actions for the item selected from the context menu can be processed in
public boolean onContextItemSelected(final MenuItem item)
For more information on context menu see here.
For a step by step tutorial visit here.
Edit
The second link is broken as it was quite old.
But I guess you can refer one of the other highly voted answer to see all the steps involved,
Another approach:
//Deleted individual cart items
//on list view cell long press
cartItemList.setOnItemLongClickListener (new OnItemLongClickListener() {
#SuppressWarnings("rawtypes")
public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) {
final CharSequence[] items = { "Delete" };
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Action:");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
cart = cartList.get(position);
db.removeProductFromCart(context, cart);
new AlertDialog.Builder(context)
.setTitle(getString(R.string.success))
.setMessage(getString(R.string.item_removed))
.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(CartDetailsActivity.this, HomeScreen.class);
startActivity(intent);
}
})
.show();
}
});
AlertDialog alert = builder.create();
alert.show();
//do your stuff here
return false;
}
});
You can call Activity.openOptionsMenu() in your click item method
check here
http://developer.android.com/reference/android/app/Activity.html#openOptionsMenu%28%29
**
after register your context menu on list view
**
override onCreateContextMenu Method like this
#Override
public void onCreateContextMenu(ContextMenu menu,View v, ContextMenu.ContextMenuInfo menuInfo){
if (v.getId() == R.id.listView){
AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo)menuInfo;
MenuItem mnu1=menu.add(0,0,0,"Delete");
MenuItem mnu2=menu.add(0,1,1,"Share");
}
}
then coding for each item that can be selected
#Override
public boolean onContextItemSelected(MenuItem menuItem){
AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)menuItem.getMenuInfo();
switch (menuItem.getItemId()) {
case 0:
Toast.makeText(this, "Delete Selected", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(this, "Share Selected", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
Use registerForContextMenu(); to link context menu to any View successor.
To access to selected ListItem data, simple use the AdapterView.AdapterContextMenuInfo.
E.g.:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo menuinfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
long selectid = menuinfo.id; //_id from database in this case
int selectpos = menuinfo.position; //position in the adapter
switch (item.getItemId()) {
case 1:
doSomething(selectid);
break;
case 2:
doSomethingElse(selectpos);
}
return super.onContextItemSelected(item);
}
A quick note for those still struggling, there're two methods
registerForContextMenu(list);
unregisterForContextMenu(list);
Make sure you pick the first.
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);
}
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
I am trying to a menu item to launch the Send intent. This is what I did, I see the menu item
but i don't see send intent launch when i select the menu item.
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
Intent sendIntent = new Intent(Intent.Action_Send);
menu.add(Menu.NONE, 0, 0, "testmenu").setIntent(sendIntent);
}
}
}
Thank you.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, 1,0,"SEND TEST");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
Intent sendIntent = new Intent(Intent.Action_Send);
switch(item.getItemId()) {
case 1:
//DO WHATEVER YOU WANT HERE
return true;
}
return super.onContextItemSelected(item);
}
Depending on what you want to send. A simple message I assume. I would do something like this in the "onContextItemSelected":
//First define up top before oncreate.
private SmsManager sm = SmsManager.getDefault();
private String number = "9995551111";
//then...
#Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case 1:
sm.sendTextMessage(number, null, "Test Message", null, null);
return true;
}
return super.onContextItemSelected(item);
}
///DONT FORGET TO ADD THE USES PERMISSION TO SEND MESSAGES IN YOUR MANIFEST!!!
You could also create an activity with views to assign a number and user input a message. and run sm.sendTextMessage with an onClickListener. You would start the activity in the "DO WHATEVER" area of the first example.
There is more info on sending SMS right Here