7

I'm trying to change the colour of the text (a string) when I output it to an email. My code is:

String appdata = "%" + txtFromSpinner + location.getText() + "%" + date.getText()+ "%" + start.getText() + "%" + finish.getText() + "%" + lunch.getText() + "%" + details.getText();
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"payrolldirectgib@gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Timesheet/Parte de horas");
emailIntent.putExtra(Intent.EXTRA_TEXT, appdata +sep+ "Please send this email."+sep+ "Your timesheet details are included in it."+sep+ "Thank you."+sep+ "Regards,"+sep+ "Admin Department."+sep+ "Payroll Direct.");
emailIntent.setType("message/rfc822");
startActivity(emailIntent);

I would like the string "appdata" to appear in red when in the email message box.

Can this be done and how?

Thank you in advance.

Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
Chris Music
  • 109
  • 11
  • You can format the email if you send it as HTML. See http://stackoverflow.com/questions/2544141/send-html-mail-using-android-intent – Colin Pickard May 26 '15 at 11:22

2 Answers2

3

There are two methods

Method 1

SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString redSpannable= new SpannableString(appdata);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, appdata.length(), 0);
builder.append(redSpannable);

Method 2

appdata_in_red = Html.fromHtml("<font color=#ff0000>" + appdate + "</font>");

I have taken the simplest method and I integrated it into your code like this:

String appdata = "%" + txtFromSpinner + location.getText() + "%" + date.getText()+ "%" + start.getText() + "%" + finish.getText() + "%" + lunch.getText() + "%" + details.getText();

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"payrolldirectgib@gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Timesheet/Parte de horas");
//this line below
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<font color=#ff0000>" + appdata + "</font>") +sep+ "Please send this email."+sep+ "Your timesheet details are included in it."+sep+ "Thank you."+sep+ "Regards,"+sep+ "Admin Department."+sep+ "Payroll Direct.");
emailIntent.setType("message/rfc822");
startActivity(emailIntent);

I hope my answer will help you.

Thomas Vos
  • 12,271
  • 5
  • 33
  • 71
  • Hi, thanks for this, but I can't seem to get it to work. I'm using Android Studio if that makes a difference. – Chris Music May 27 '15 at 20:54
  • I've tried adding the following: emailIntent.setType("text/html"); and changed the text line to read: emailIntent.putExtra(Intent.EXTRA_HTML_TEXT, ("" + " PLEASE IGNORE THIS DATA ==> " + myWifiInfo.getSSID() + appdata + "<==" ) ); – Chris Music May 28 '15 at 08:16
0

Use code as shown below:

emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<font color='#FE2B3C'>"+appdata+"</font>"+sep+"Please send this email."));
Ankii Rawat
  • 2,070
  • 17
  • 17