How can I add a menu item to launch the Send intent - android

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

Related

Passing information to another Activity using intent on click of Context Menu

I am trying to send extra information using put extra, to another activity when the user clicks a context menu. The context menu loads the activity but the information does not appear on the other activity. The code below shows what I have tried.
public void in(int position) {
// gets the position of the item in my array list
Cars c = cars.get(position);
Intent i = new Intent(this, DisplayMessageActivity.class);
i.putExtra("TITLE", c.getCarsTitle());
startActivity(i);
}
// create context menu
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.cars_menu_layout, menu);
}
// runs on long click
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.register:
// runs the method that starts the intent
in(0);
return true;
case R.id.view:
return true;
default:
return super.onContextItemSelected(item);
}
}
try using
i.putExtra("TITLE", c.getCarsTitle().toString);

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

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

get view id from oncontextitemselected

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

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