Search code examples
androidandroid-studiotextviewnewline

Android - TextView.setText string with \n problem


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.