Android – TextView.setText string with \n problem

Issue

I have a method which retrieves some text from a Firebase:

    db = FirebaseFirestore.getInstance();
    db.collection("Contact").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (DocumentSnapshot document : task.getResult()) {
                    String textt = (document.getString("Text"));
                    settext(textt);
                }
            }
        }
    });

When I retrieve the String "Text" I call the method settext:

private void settext(String textt){
    mainTextView.setText(textt);
}

Here is the value of the string text: "azerty123\nqwerty\ntest"
But the mainTextView prints "azerty123\nqwerty\ntest" without newlines.

Can someone help me?

It’s FIXED, just override the method settext to:

private void settext(String textt){
    String text = textt.replaceAll("\\\\n", "\n");
    mainTextView.setText(text);
}

Solution

Try this inside onComplete():

String textt = document.getString("Text").replaceAll("\\\\n", "\n");

or

String textt = document.getString("Text").replace("\\n", "\n");

just in case the string coming from the db contains special characters.

Answered By – forpas

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published