Android TextView new line isn't working

android textview new line not working
android textview multiple lines programmatically
android textview line break mode
android textview automatic line break
android textview wrap text multiple lines
android textview unlimited lines
how to set max line in textview android
set number of lines in textview android

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.

  • Hardcoding the string value directly into setText()
  • Various combinations of all of the above
  • removing android:clickable="false"
  • removing android:cursorVisible="false"
  • removing android:focusable="false"
  • removing android:focusableInTouchMode="false"

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

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

How do I add a newline to a TextView in Android?, To add a newline between texts in Android TextViews we can make use of the escape sequence that we use in Java Programming i.e "\n" (Slash followed by  I want the TextView hold the content it's already got and show it. And every time the incoming data it's just one more string i want print in new line. You see now the code above is appending the StrM with new incoming data string and then the TextView reprints evry time entire StrM.

\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

How to add Newline to text in Android TextView, This example demonstrates how do I add a line break in an android textView.​Step 1 − Create a new project in Android Studio, go to File ⇒ New  I did like below in xml <TableRow> <TextView android:id="@+id/address1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="

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>

How to create new line and brake line textview in Android studio , You can change lines using new line character '\n'. Place this where the lines need to be changed. Example: XML : <TextView  There you can define how many lines the TextView should have, and if there should be displayed dots ("") when the text exceeds the TextView size. Also, you can use returninside your strings .xml to start a new line: (" ") <string name="intro">This is the first line this is a new line.</string>.

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

How to add a line break in an android textView?, Now just insert new-line character to have a line-break in your view. 1. textView.​setText( "First line \nSecond line \nThird line" );. Have fun! @DavidDraughn actually I needed a text view with a fixed four lines and if I put space between Vishal and Vyas in this statement like android:text="Vishal Vyas Having 342 Reputation", some times the word "Reputation" was not displayed because it was shifted to the 5th line.

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);

How to set the multiple lines in TextView Android Studio, Android. I have a multiline textview need to show the status of the server. So it should always add a new line when server status changes. With Android 8.0 (API level 26) and higher, you can instruct a TextView to let the text size expand or contract automatically to fill its layout based on the TextView's characteristics and boundaries.

Set line-break in TextView, This week it's all about the small tricks around Android's TextView . resource and will cleanly break the text in your TextView to the next line. Here's how to activate your new iPhone or Android smartphone. You can also learn how to turn off iMessage, transfer content and more.

How to dynamically append a new line to a multiline textview , let]s say I have incoming data which is new evry time I grab it then I need to print that In text view fron the new line \n but what ever I do it's  I am trying to add a line break in the TextView. I tried suggested but that does nothing. Here is how I set my texts. TextView txtSubTitle = (TextView)findViewById(r.id.txtSubTitle); txtSubTitle.setText(Html.fromHtml(getResources().getString(R.string.sample_string)));

Android Quick Tips #1,

Comments
  • after setting text do like this messageTextView.setText(messageTextView.replace("\\n", "\n"));
  • Can you add XML code here for you layout which contains TextView
  • Can you post <TextView> xml code here
  • this is the best way to manage the textview like padding, and margin propoerty to set message layout .
  • Do not use ems and inputType.
  • Can you think of any reason why it wouldn't work for me? Would it be something with my environment?
  • The \r is not necessary. Android is Linux based, \n is a newline.
  • Which platform are you using for Android Studio? Windows or Linux
  • Windows, it was stated in my question
  • @JayPatel First off, its \r and ]n, not forward slash. Secondly, Android is a linux OS. That's what matters- the platform you run on, not what platform you compile on. That means you only need the linux newline, \n
  • \n doesn't work for me, neither does System.getProperty("line.separator") The string must be passed in programmatically so I cannot harcode the string into XML