Reputation: 109
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[]{"[email protected]"});
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.
Upvotes: 7
Views: 2337
Reputation: 12571
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[]{"[email protected]"});
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.
Upvotes: 3
Reputation: 2090
Use code as shown below:
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<font color='#FE2B3C'>"+appdata+"</font>"+sep+"Please send this email."));
Upvotes: 0