How to replace the characher `\n` as a new line in android
Asked Answered
E

6

9

I have one server response for an API request as shown below.

Success! Your request has been sent.\n\nWe’ll inform you once it is done.

This message I need to show in a Snackbar. I need new line to be added in the place of \n in this response . I tried by using replaceAll like

String message = (serverResponse.getMessage()).replaceAll("\\n", System.getProperty("line.separator"));

but it is showing like this

enter image description here

Same message if I add in string.xml resource file and get using getString(R.string.message) then the \n is working properly. How can I get a new line from this response string?

I tried changing \n with other character like <new_line> from server response and it is working fine with replaceAll. Problem is only with \n in response message. Is there any way to parse \n?

Ethical answered 22/12, 2015 at 14:0 Comment(8)
You don't have to replace it. \n will make new lines automatically.Hudnut
"\\n" will be shown as "\n" and "\n" will be shown as a new line.Cystolith
tried but not working. same resultEthical
@Jrd I posted a solution below. Your method is correct, but the Design Libary's SnackBar will enforce only 2 lines....but there are solutions to this stillMccarter
I've already set max lines for snackbar as 10 linesEthical
Did it work? Did you do all my tests and solutions? Just remember that Snackbars are only meant for a brief message.Mccarter
no didn't work. now instead of \n I'm sending <new_line> from serverEthical
If you have control of the server, then you should provide more concise and readable messages. Check out the Google Documentation for messages to users.Given your example, there really isn't a reason to expand the Snackbar beyond 2 lines. I mean Google is forcing that constraint for a reason.Mccarter
D
18

What you need is

String message = serverResponse.getMessage().replaceAll("\\\\n", "\n");

Why four backslashes are needed?

Because in Java backslash \ is escape character. If you want to have single backslash literal inside Java string you have to escape it and use \\

But, replaceAll method expects regex expression, where again backslash is escape character so you need to escape it, too.

Basically, in above code Java string parser will first convert those four backslashes to two \\\\ -> \\ and then regex parser will interpret remaining two backslashes as single backslash literal.

Defluxion answered 22/12, 2015 at 14:20 Comment(1)
That worked for me, but why is it replaceAll("\\\\n", "\n") instead of replaceAll("\\n", "\n")?Michel
F
5

I believe you should be able to accomplish by doing the following.

// Replace "\n" with "<br>"
String message = (serverResponse.getMessage()).replaceAll("\\n", "<br>");

// Now set SnackBar text using HTML
mSnackBar.setText(HTML.fromHTML(message)) 

By using HTML.fromtHTML(String) you should be able to keep any formatting, such as breaks, ASCII HTML characters (bullets, stars, ect.), coloring and/or bolding/italicizing! I use this quite often to format text in TextViews that I have displayed to users. Do not see why it wouldn't work with SnackBars!

Furuncle answered 22/12, 2015 at 14:12 Comment(0)
M
2

The Support Design Library will force only 2 lines for the Snackbar. This correlates to around 80dp max size.

Your solution should work, and is correct. Try it out in a Toast for a quick test. It will work as your expect. Another test you can do is to get rid of one of the \n, then it will probably display correctly; however, there are a few other options you can do for. Again, these are just tests. Check below for some real solutions!

Solutions

  • Remove all the \n from the Snackbar text. This is probably the best solution as it will allow your design to remain as close to Material as possible. Highly Recommended

  • You can get the actual TextView from the Snackbar, and modify its max number of lines

View sbv = snackbar.getView(); TextView tv = (TextView) sbv.findViewById(android.support.design.R.id.snackbar_text); tv.setMaxLines(5);

  • In XML, you can modify the attribute that affects the Design Library's Snackbar number of lines. Not Recommended at all. This name can change without notice and break your UI

<integer name="design_snackbar_text_max_lines">5</integer>


Edit

If you have access to modify the contents of the server response, then I Highly highly highly suggest that you modify the returned server response to be way more concise to the user. Your current message is not concise and takes the user longer than needed to read.

Change it to this..

Request sent! You will be informed shortly.

I would actually find a better work for Request if you can. For example, if they ordered pizza and sent a request, then you ould say Order sent! .... Also, you might need to modify shortly to be more accurate to what a user can expect. Shortly, to me, means I should expect something within the hour at the very latest.

Anyways, check out this documentation. It is higly recommended for writing styles on Android. https://www.google.com/design/spec/style/writing.html#writing-language Source: Android Multiline Snackbar

Mccarter answered 22/12, 2015 at 14:12 Comment(0)
H
0
 Snackbar snackbar =  Snackbar.make(ref_id, "Success! Your request has been sent.\n\nWe’ll inform you once it is done.",
                Snackbar.LENGTH_LONG).setDuration(Snackbar.LENGTH_LONG);
        View snackbarView = snackbar.getView();

        TextView tv= (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);

        tv.setMaxLines(3);

        snackbar.show();
Hern answered 14/10, 2016 at 13:17 Comment(0)
B
0

How about this, Jrd.

strings.xml :

<string name="br">\n</string>

snackbar :

"Your request has been sent.." + getResources().getString(R.string.br)
Bluebell answered 26/10, 2016 at 6:56 Comment(0)
R
0
StringBuilder strAppend = new StringBuilder();
strAppend.append("\n");

String newString = oldString.replace("\\n", strAppend);

Log.d(TAG, "new: " + newString );
Rahel answered 5/6, 2019 at 15:24 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Pair

© 2022 - 2024 — McMap. All rights reserved.