How to insert line break in String to display in Android TextView widgets?

When I set the textView line manually as shown below, it works.

<TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="line 1 \n line 2"/>

However, when I try to show a String in this text element, it does not jump to a new line.

String sample = "line 1 \n line 2";
textView1.setText(sample);

Note. In fact, I get the row from the SQLite database. But it should not matter? Beacause is another line!

sample = cursor.getString(DBAdapter.COL_sample);
textView1.setText(sample);

So now my TextView shows the character "\ n" or "\ r \ n" instead of a line break.

+4
source share
3 answers

So now, since you are saying this from a SQLite database, the problem is there.

Blundell .

, SQLite , , , \\\n , unescape ( , \n.

, , , .

, , - ( , ):

textView1.setText(sample.replaceAll("\\\\n", "\n"));

, .

+3

, strings.xml( cmd + 1).

, .

+1

You can try

sample = sample.replace("\\\n", System.getProperty("line.separator"));
0
source

Source: https://habr.com/ru/post/1537248/


All Articles