Android ACTION_SEND - android

i wanna make an action that the android app can send an email, and the email subject and body already fill with the edittext from the android app.
here's my code
Intent ii = new Intent(android.content.Intent.ACTION_SEND);
ii.setType("text/plain");
EditText txtName = (EditText)findViewById(R.id.txtName);
EditText txtAddress = (EditText)findViewById(R.id.txtAddress);
EditText txtLatitude = (EditText)findViewById(R.id.txtLatitude);
EditText txtLongitude = (EditText)findViewById(R.id.txtLongitude);
ii.putExtra(android.content.Intent.EXTRA_SUBJECT, txtName.getText().toString());
ii.putExtra(android.content.Intent.EXTRA_TEXT, txtAddress.getText().toString());
ii.putExtra(android.content.Intent.EXTRA_TEXT, txtLatitude.getText().toString());
ii.putExtra(android.content.Intent.EXTRA_TEXT, txtLongitude.getText().toString());
startActivity(Intent.createChooser(ii, "Share via"));
The subject is fine, but the body email is only fill with the last content(txtLongitude).
please tell me what to do.
Gratz.

You need to first make one string from the 3 EditTexts, then set it as an extra. For Example:
String body = txtName.getText().toString() + txtLatitude.getText().toString() + txtLongitude.getText().toString()
ii.putExtra(android.content.Intent.EXTRA_TEXT, body);
You'll probably also want to add some things like new line characters to format the text.

The subject is fine, but the body email is only fill with the last content(txtLongitude).
That is because you keep overriding the same EXTRA_TEXT extra. Call putExtra() for EXTRA_TEXT once, with the complete body of the email you want to send.

Related

Calling a phone number provided by GooglePlaces in Android

So using google places reference (detailed web-service) i retrieved a "formatted phone number" its in the form of (256) 922-0556. The goal is to call this number. The way I am trying is be using an intent. However, the number above is not a in the format to use Uri parse method. Anyone know a solution to call this number? Is there a different intent or a good way to turn this into Uri data? I have seen the opposite of this done like so:
1234567890 → (123) 456-7890
String formattedNumber = PhoneNumberUtils.formatNumber(unformattedNumber);
But i want to do the reverse of this. any ideas or alternative solutions?
Here is my code:
protected void onPostExecute(Boolean result){
Intent callintent = new Intent(Intent.ACTION_CALL);
callintent.setData(Uri.parse(phoneNum));
try {
startActivity(callintent);
}catch (Exception e) {e.printStackTrace();}
}
Where phoneNum is a formatted phone number string retrieved from GooglePlaces via JSON
To expand on Peotropo's comment: is there a better way to replace values than the following?
phoneNum = phoneNum.replace(" ", ""); // gets rid of the spaces
phoneNum = phoneNum.replace("-", ""); // gets rid of the -
phoneNum = phoneNum.replace("(", ""); // gets rid of the (
phoneNum = phoneNum.replace(")", ""); // gets rid of the )
This is simple string. Use String.replace() method to remove extra chars.
You can also use replaceAll method:
String phoneNumber = "(123)123-456465"
return phoneNumber.replaceAll("[^0-9]", "");
Not tested docs are here:
replaceAll
Java regular expressions
You don't need to do a string replace. You can use the Spannable code below to have your phone automatically recognize the number and call it. It adjusts for parentheses, spaces and dashes.
// call the phone
SpannableString callphone = new SpannableString("Phone: " + phone);
callphone.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
callphone.setSpan(new URLSpan("tel:"+phone), 7, 21, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView zphone = (TextView) findViewById(R.id.phone);
zphone.setText(callphone);
zphone.setMovementMethod(LinkMovementMethod.getInstance());
It will display
Phone: (123) 456-7890
Where you see 7,21 in the code above it is saying to start at the 8th character, which is the ( and end at the 21st character which is the last digit in the phone number. Adjust it to display how you want.
Nothing special to do in your view:
<!-- Phone Number Label -->
<TextView
android:id="#+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"/>

Using HTML in a String

In my application I have to use a some HTML stuff in a string. But HTML is not working as intended. I have to use that string (Text) to send as an email. The sequence I required of HTML is:
Title (in the center)
Image (in the center)
Description (left align)
and then this HTML string is passed to an email intent. But neither image is showing up in the email nor the title text is getting center align. This is how I am doing this all:
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "");
it.setType("text/html");
String title = title;
String emailText = emailText;
it.putExtra(Intent.EXTRA_SUBJECT, title);
it.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(emailText));
this.startActivity(it);
and this is how the emailText is being formed:
emailText = "<p style= 'color:#000000; font:Georgia; font-size:18pt; text-align:center' align = 'center'><b>" + title +" </b></p>"
+"<br/><br />"
+"<img style=\"border:3px solid #173E8C\" src=\'" +imageUrl+"\' width=\"120\" height=\"90\"align=\"center\"/>"
+"<br/><br/>"
+"<p>" + description;
But I am unable to get the required result that I have mentioned right at the top, Any help is appreciated related to the issue. Thanks in advance..:-)
You must specify the type of email through the function setType () :
it.setType("text/html"); // for HTML
it.setType("text/plain"); // for plain text
You can't send a image as email body in android through Intent.

Android SMS Intent not sending full message, other options?

I'm sending an SMS from my app using an Intent. The SMS pulls a string with "\n" and "\t" formatting out of a text view to send out to a contact of the user's choosing.
My problem is that when sending from a device to another device, the SMS only sends the first 15 characters of the message! When the message appears on the first device, it has all the characters in the string. Then only the first 15 of those characters are received by the second device.
// SMS Activity
public void sendSMS(View v) {
mess = ""+Summary.getText().toString();
Uri uri = Uri.parse("smsto:");
Intent txt = new Intent(Intent.ACTION_SENDTO, uri);
txt.putExtra("sms_body", mess);
startActivity(txt);
}
This is how I generated the string for the message. The text view is also being used for showing the string in the App.
for(...){
if{...}
else{
String beer = name[counter];
int amount = amt[counter];
inText += "x"+ amount+ "\t" + beer + "\n";
}
}
Summary.setText(""+inText);
mess = ""+Summary.getText().toString();
Is there a different method to go about sending this string through sms? I know I can do it using SmsManager, but I want to be able to use the phones messaging system (easier because they can choose the contacts). Any examples I can follow?
Could I use a cursor to get the information for the string?
try adding txt.setType("text/plain");
this might solve your problem.

Html Display in Email Body Android

I want to display a table in email body. The code is as below.
stringBuilder.append("<html>" +
"<body>"+
"<table border=\"1\">"+
"<tr>"+
"<th>Name</th>"+
"<th>Telephone</th>"+
"<th>Telephone</th>"+
"</tr>"+
"<tr>"+
"<td>Bill Gates</td>"+
"<td>555 77 854</td>"+
"<td>555 77 855</td>"+
"</tr>"+
"</table>"+
"</body>"+
"</html>");
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Checklist Information");
intent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(stringBuilder.toString()));
But the table is not showing, rather a simple text is showing in mail body.
Please help me out.
the table html tag is not supported by Html.fromHtml(). See here

Removing space from Edit Text String

In my android app, I am getting the String from an Edit Text and using it as a parameter to call a web service and fetch JSON data.
Now, the method I use for getting the String value from Edit Text is like this :
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
Now normally it works fine, but if we the text in Edit Text contains space then my app crashes.
for eg. - if someone types "food" in the Edit Text Box, then it's OK
but if somebody types "Indian food" it crashes.
How to remove spaces and get just the String ?
Isn't that just Java?
String k = edittext.getText().toString().replace(" ", "");
try this...
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
String newData = k.replaceAll(" ", "%20");
and use "newData"
String email=recEmail.getText().toString().trim();
String password=recPassword.getText().toString().trim();
In the future, I highly recommend checking the Java String methods in the API. It's a lifeline to getting the most out of your Java environment.
You can easily remove all white spaces using something like this. But you'll face another serious problem if you just do that. For example if you have input
String input1 = "aa bb cc"; // output aabbcc
String input2 = "a abbcc"; // output aabbcc
String input3 = "aabb cc"; // output aabbcc
One solution will be to fix your application to accept white spaces in input string or use some other literal to replace the white spaces. If you are using only alphanumeric values you do something like this
String input1 = "aa bb cc"; // aa_bb_cc
String input2 = "a abbcc"; //a_abbcc
String input3 = "aabb cc"; //aabb_cc
And after all if you are don' caring about the loose of information you can use any approach you want.

Categories

Resources