Skip to content Skip to sidebar Skip to footer

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 fi

Solution 1:

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

finalTextViewnline=newTextView(this);
nline.setSingleLine(false);
nline.setText("first line\n"+"second line\n"+"third line");

Solution 2:

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 /><Viewandroid:background="#00000000"android:layout_height="12dp" //orwhateverdensitypixelheightyouwantandroid:layout_width="fill_parent" /><TextView />

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

nline.setText(" \n");

Solution 3:

You may need to set the InputType to TYPE_TEXT_FLAG_MULTI_LINE using the setInputType() method of TextView

tv.setInputType(tv.getInputType()|InputType.TYPE_TEXT_FLAG_MULTI_LINE);

You also could just set some margin/padding on the other views. I think that TextViews should not be misused as spacers.

Solution 4:

Simple as:

String hello = getResources.getString(R.string.hello);
String world = getResources.getString(R.string.world);
textView.setText(hello + "\n" + world);

Solution 5:

Simple by giving "\n" data is displayed in a new line

txtView.setText("Latitude :"+ latitude +"\nLongitude :"+ longitude);

Post a Comment for "Java Android: Appending A Newline Using Textview"