Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a newline to a TextView in Android?

People also ask

How do I add a line break in Android?

Add Line Breaks to a TextView Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.

How do you add multiple lines in TextView?

When you have a text that's longer than one line, then the TextView will automatically put the text in multiple lines. When you set the layout_width and layout_height as wrap_content or match_parent , then the TextView widget will use all the available space to display the text you specified as its content.

How do you go to the next line in EditText?

ADD this to your EditText xml code This will automatically moves your text to next line while entering data. this doesn't add \n chars to the string. just breaks the line in the EditText view, which isn't what the OP is asking for.


Don't trust the Visual editor. Your code does work in the emu.


Try:

android:lines="2"

\n should work.


try System.getProperty("line.separator");


I think this has something to do with your HTM.fromHtml(subTitle) call: a "\n" doesn't mean bupkis to HTML. Try <br/> instead of "\n".


First, put this in your textview:

android:maxLines="10"

Then use \n in the text of your textview.

maxLines makes the TextView be at most this many lines tall. You may choose another number :)


Tried all the above, did some research of my own resulting in the following solution for rendering line feed escape chars:

string = string.replace("\\\n", System.getProperty("line.separator"));
  1. Using the replace method you need to filter escaped linefeeds (e.g. '\\\n')

  2. Only then each instance of line feed '\n' escape chars gets rendered into the actual linefeed

For this example I used a Google Apps Scripting noSQL database (ScriptDb) with JSON formated data.

Cheers :D


Make sure your \n is in "\n" for it to work.