Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email intent ignoring line-breaks in preset text (Android)

In various Android applications, I use the following code to show an application chooser for email and, after the user has decided for one of the apps, insert a predefined text into the email form:

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample subject");
String contentStr = "";
for (Object o : mArrayList) { // mArrayList: ArrayList<Object>
    content = contentStr+o.toString()+"\n";
}
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, backupStr);
startActivity(Intent.createChooser(emailIntent, "Choose application"));

In the for-loop, the objects' string output is concatenated with the temporary string "contentStr". After every object, there should be a line break ("\n").

So when I test this code on my phone, it works fine and every single object has its own line.

But users report that their email application (Android standard as well) puts everything in one line and ignores the line breaks.

So am I doing anything wrong? Or can I just ignore this bug report as its not an issue which the developer can solve?

like image 632
caw Avatar asked Mar 14 '12 22:03

caw


2 Answers

2 potential leads :

  • Try with \r or \n\r instead of simply \n, or even 2 line breaks.
  • Use HTML formatted line breaks (<br>)

Also, there is something wrong with your code in here

String contentStr = "";
for (Object o : mArrayList) { // mArrayList: ArrayList<Object>
    content = contentStr+o.toString()+"\n";
}
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, backupStr);

This does not put anything in the intent, contentStr is empty, and content only contains the last object.

This is a better, shorter and more efficient method:

contentStr = TextUtils.join("\n", mArrayList);
like image 196
njzk2 Avatar answered Oct 13 '22 01:10

njzk2


You need replace \n to <br/> even you put EXTRA_TEXT. There is my code:

final Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "This\nis\na\ntest!".replace("\n", "<br/>"););
like image 37
justbilt Avatar answered Oct 13 '22 01:10

justbilt