Add new line character in remote config text
Asked Answered
D

7

15

I'm doing Firebase RemoteConfig integration. In one of the scenarios, I need to break a text line, so I tried to use new line character (\n).

But this is not working, it is neither displaying as an extra character nor creating another line.

Diseased answered 10/4, 2018 at 6:6 Comment(4)
Did you tried o<br /> from this answerBulb
@Bulb after your suggestion tried that but not working :-/Diseased
@CoDe, did you find an answer for this?Lantha
nope, but fixing can be done with Mayank solution below.Diseased
C
13

My solution is replace \n manually (assuming that in Firebase Console you put property for TITLE as "Title\nNewLine"):

FirebaseRemoteConfig.getInstance().getString(TITLE).replace("\\n", "\n")
Clomp answered 25/6, 2019 at 16:4 Comment(0)
R
1

Try using an uncommon character like two pipes || and then replacing every occurance of those with a newline after you do getString() in the code.

Riser answered 20/4, 2018 at 20:49 Comment(1)
Why wouldn't you just replace occurrences of \n?Duwe
S
1

The trick (which actually works for all HTML tags supported on your target platform) is to wrap the String in a JSON Object on RemoteConfig, like so:

{
  "text":"Your text with linebreaks...<br><br>...as well as <b>bold</b> and <i>italic</i> text.
}

On the target platform you then need to parse the JSON and convert it back to a simple string. On Android this looks like this:

// extract value from JSON
val text = JSONObject(remoteConfig.getString("remoteConfig_key")).getString("text")

// create Spanned and use it
view.text = HtmlCompat.fromHtml(text)
Selfness answered 19/12, 2019 at 17:33 Comment(0)
R
0

To make the suggestion mentioned above, you can try this code(that can be generalized to "n" number of elements). Simply replace the sample text with yours with the same format and add the amount of elements

String text="#Elemento1#Elemento2#Elemento3#";
int cantElementos=3;
arrayElementosFinales= new String[cantElementos];
int posicionNum0=0;
int posicionNum1;
int posicionNum2;

for(int i=0;i<cantElementos;i++){
    posicionNum1=text.indexOf("#",posicionNum0);
    posicionNum2=text.indexOf("#", posicionNum1+1);
    char [] m = new char[posicionNum2-posicionNum1-1];
    text.getChars(posicionNum1+1, posicionNum2,m,0);
    arrayElementosFinales[i]=String.valueOf(m);
    posicionNum0=posicionNum2;
}
Rags answered 12/9, 2018 at 4:0 Comment(0)
C
0

You can insert encoded text(with Base64) to Firebase panel.

After, decode the String from your Java class and use it.

Like

byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
Chlorophyll answered 22/1, 2019 at 13:49 Comment(0)
D
0

So what worked for me is to use "||" (or some other character combination you are confident will not be in the string) as the new line character. Then replace "||" with "\n". This string will then display properly for me.

For some reason sending "\n" in the string doesn't get recognized as expected but adding it manually on the receiving side seems to work.

Disciple answered 5/3, 2019 at 16:41 Comment(0)
S
-2

Use Cdata in the remote config in combination with "br" tag and HTML.fromHtml() .. for eg.

<![CDATA[ line 1<br/>line 2]]>
Safari answered 24/7, 2018 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.