How do I add a newline to a TextView in Android?
Don't trust the Visual editor.
Your code does work in the emu.
Java android: appending a newline using TextView
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");
How to add a line break in an Android TextView?
ok figured it out:
<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
Android TextView new line isn't working
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"
Android TextView - Add A New Line After Every 20 Characters
With the help of @Der Golem provided link, I try to make one solution. Idea is that you need to set text from class file. And append '\n' character after every 20 character.
Variables required,
TextView textView;
String tmpString = "1234567890123456789012345678901234567890";
StringBuffer finalString;
Block of code that you need to write in onCreate method.
textView = (TextView) findViewById(R.id.txt);
int index = 0;
finalString = new StringBuffer();
while (index < tmpString.length()) {
Log.i(TAG, "test = " + tmpString.substring(index, Math.min(index + 20,tmpString.length())));
finalString.append(tmpString.substring(index, Math.min(index + 20,tmpString.length()))+"\n");
index += 20;
}
textView.setText(finalString);
Update : Reference,
String mainString = "Hi Android, How are you? I hope you are doing great.";
String[] stringArray = mainString.split("\\s+");
String tmpString = "";
for (String singleWord : stringArray) {
Log.d(TAG, "singleWord = " + singleWord);
if ((tmpString + singleWord + " ").length() > 20) {
finalString.append(tmpString + "\n");
Log.e(TAG, "finalString = " + finalString);
tmpString = singleWord + " ";
} else {
tmpString = tmpString + singleWord + " ";
}
Log.d(TAG, "tmpString = " + tmpString);
}
if (tmpString.length() > 0) {
finalString.append(tmpString);
Log.e(TAG, "last finalString = " + finalString);
}
textView.setText(finalString);
Not a great solution but may be meets your requirement. Let me know if you need further assistance.
TextView go to new line in space
You need to observe layout changes in this case since the due to height change the bottom value will be changed if the text set to two line.
work with wrap_content only
TextView tvDevice = findViewById(R.id.ObuTransitElementObuDeviceCode);
tvDevice.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if(bottom != oldBottom){
String s = tvDevice.getText().toString();
if(!s.contains("\n"))tvDevice.setText(s.replace(":",":\n"));
}
}
});
tvDevice.setText("Number of device:\n"+dynamic_number);
How do I insert a line break in a String to show on an Android TextView widget?
Ah so now since you say it's from the SQLite database, that's the issue there.
You can refer to the answer by Blundell here.
The issue is that SQLite will escape your new line characters so when you retrieve it again, it will come out as \\\n
or equivalent in which you will need to unescape to display properly (so remove the extra slash so it is just \n
.
So depending on what new line characters are being saved into your database, you will need to handle accordingly.
So using the linked answer, you could do something like this (will depend on the number of backslashes and other characters you need to do):
textView1.setText(sample.replaceAll("\\\\n", "\n"));
If you need to do it in multiple places create a function for this.
How to have HTML New Line Feature in Android Text View?
Having this string as an example:
String sBooks = "Books on Chennai:\n ....";
Replace: \n
with <br>
:
sBooks = sBooks.replace("\n", "<br>");
Example using a TextView
:
myTextView.setText(Html.fromHtml(sBooks));
Related Topics
How to Figure Out Which Sim Received Sms in Dual Sim Android Device
Onclick() to Change Button Border Color Rather Changing Button Color
How to Close the Current Fragment by Using Button Like the Back Button
Photo Rotated from Camera (Samsung Device)
How to Open Draw Overlay Permission Popup in Miui
How to Execute Code When Application Is Closing
Adb.Exe Not Found After Installing Android Studio
How to Delete or Remove Cardview from Recyclerview - Android Studio
How to Check If a Value Exists Already in a Firebase Data Class Android
Reload Recyclerview from Within the Adapter of the Recycler View
Why Livedata Observer Is Being Triggered Twice for a Newly Attached Observer
Waiting Till the Async Task Finish Its Work
Suppress Keyboard from Popping Up
How to Set Android_Ndk_Home So That Android Studio Does Not Ask for Ndk Location
Get City Name and Postal Code from Google Place API on Android
How to Remove Android Auto-Suggest Underlining in Edittext
React Native: Java_Home Is Not Set and No 'Java' Command Could Be Found in Your Path
How to Select Only One Checkbox in Recyclerview and Notifydataset Changed