pianoisland
pianoisland

Reputation: 173

Android TextView new line isn't working

I found the Answer I will accept it in 2 days time

I have been trying to create an activity that can display multiple messages for the user, in order to fit everything in the text view I need to create three lines. My search online hasn't given me any solutions, here is what I have tried

Java

"\n"
"\r\n"
newLineChar = System.getProperty("line.separator")
messageTextView.setText(Html.fromHtml("This\n Is\n A Test"));
messageTextView.setText(Html.fromHtml("This<br> Is<br> A Test"));

xml

android:lines="3"
android:maxLines="3"

Misc.

Code snippet:

// Message passed to next activity via putExtra()
message = "This\n Is\n A Test";
// Next Activity
TextView messageTextView = (TextView) findViewById(R.id.messageToUser);
String message = getIntent().getStringExtra("message");
messageTextView.setText(message);

Current and Updated XML for the TextView:

<TextView
    android:id="@+id/messageToUser"
    android:layout_width="0dp"
    android:layout_height="200dp"
    android:background="#90FFFFFF"
    android:ems="10"
    android:fontFamily="serif"
    android:text=""
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="30sp"
    android:textStyle="bold"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    android:layout_marginStart="165dp"
    android:layout_marginEnd="165dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="209dp"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

If this helps, I'm on Windows 7, Android Studio 2.3.2, Java 1.8, designing an App Specifically for SM-T580 (Samsung Tab A 10.1"), the TextView's parent is the base ConstraintLayout of the component tree

Upvotes: 6

Views: 26096

Answers (9)

pianoisland
pianoisland

Reputation: 173

Found the issue I had to take android:inputType="textPersonName" out of the XML for the TextView. I had mistakenly left the Html.fromHtml(...) method call when I initially removed android:inputType="textPersonName". After calling message.setText("This\n Is\n A Test"); with NO android:inputType="textPersonName".Everything worked as expected.

For final clarity....

The newline character "\n" will not work if the input type is "textPersonName"

The newline character for Andriod is "\n"

Upvotes: 1

Devbrat Dash
Devbrat Dash

Reputation: 41

Tried many finally this worked for me

messageTextView.setText(string.replace("\n", ""+System.getProperty("line.separator")));

Upvotes: -2

Jeanne vie
Jeanne vie

Reputation: 524

There is no need to set for the android:inputType because this is a TextView. These are the steps that I did to successfully implement the new line:

  1. Make sure that android:maxLine is not set to 1. If you set this to 1, it will not implement new line. [Refer to the screenshot]

set maxLine inside xml file

  1. If you want to manipulate a string that is not constant or not declared inside your string.xml, put System.getProperty("line.separator") between two string. In my case I put

    phoneNumber = memberProfile.getPhoneNumber() + System.getProperty("line.separator") + "09162343636";

  2. If you have a constant string or you have set it to your string.xml file, just put "\n" and that works perfectly. Refer sample below:

    string.xml

I was using a recyclerView to display so here is the output of this:

Output

Upvotes: 4

Farrukh Najmi
Farrukh Najmi

Reputation: 5316

This works for me:

messageTextView.setText(Html.fromHtml("This<br/>Is<br/>A Test"));

Explanation: The TextView content is HTML content. <br/> is an HTML tag for a line break.

Upvotes: 2

Fakhriddin Abdullaev
Fakhriddin Abdullaev

Reputation: 4940

Add this code to res/strings

<string name="myhtml">
<![CDATA[
<p>This is a <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph of the same string.</p>
]]>
</string>

Add this your Activity class

TextView tv = (TextView)findViewById(R.id.foo);
tv.setText(Html.fromHtml(getString(R.string.myhtml)));

Upvotes: 1

AskNilesh
AskNilesh

Reputation: 69754

try this \n corresponds to ASCII char 0xA, which is 'LF' or line feed

        tv.setText("First line " + System.getProperty("line.separator") + "Line 2"+ System.getProperty("line.separator") + "Line 3");


String String1 = "value 1";
String String2 = "value 2";
TextView.setText(String1 + "\n" + String2);

or try this

string = string.replace("\\\n", System.getProperty("line.separator"));

for hardcore string try this

<string name="value"> This\nis a sample string data</string>

or

<string name="value> This<br>is a sample<br> String data</string>

Upvotes: 5

Jay Patel
Jay Patel

Reputation: 2451

\r\n works for me

messageTextView.setText("First line\r\nNext line");

Or alterantively you can also use string variable

<string name="sample_string"><![CDATA[some test line 1 <br />some test line 2]]></string>

so wrap in CDATA is necessary and breaks added inside as html tags

Upvotes: 2

squalle0nhart
squalle0nhart

Reputation: 351

try this. It work for me

message = "This"+System.getProperty("line.separator") 
+ "Is" + System.getProperty("line.separator") + "A Test";
// Next Activity
TextView messageTextView = (TextView) findViewById(R.id.messageToUser);
String message = getIntent().getStringExtra("message");
messageTextView.setText(message);

Upvotes: 1

Ramees
Ramees

Reputation: 76

messageTextView.setText(Html.fromHtml("This\n Is\n A Test"));

Upvotes: 1

Related Questions