Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java android: appending a newline using TextView

I just want to add a new line somehow to my linear layout:

layout = (LinearLayout) findViewById (R.id.layout);  

... //some other code where I've appended some strings already

final TextView nline = new TextView(this);
nline.setText(Html.fromHtml("<br>")); //i also tried:  nline.setText("\n");
layout.addView(nline);

But this just adds a few spaces. Can someone help me out? Thanks.

like image 605
maxcollins Avatar asked May 27 '11 22:05

maxcollins


2 Answers

First you need to make your TextView to be multiline. And then use simple "\n" string for linebreak.

final TextView nline = new TextView(this);
nline.setSingleLine(false);
nline.setText("first line\n"+"second line\n"+"third line");
like image 123
inazaruk Avatar answered Oct 01 '22 09:10

inazaruk


If you just want to have some empty space between two other views, you could do this in your XML (assuming you're using XML for the layout). Something like this could work, basically putting in a View with a transparent background and given height. This is assuming you have whatever parameters you want in your TextViews.

<TextView />

<View android:background="#00000000"
      android:layout_height="12dp" //or whatever density pixel height you want
      android:layout_width="fill_parent" />

<TextView />

Also, in what you tried above... you could try a space and newline... that might work.

nline.setText(" \n");
like image 40
Maximus Avatar answered Oct 01 '22 07:10

Maximus