Character for inserting line break instead of creating paragraph in Android - android

Is there a character for inserting line break without creating new paragraph in TextView?
Really I have a bidirectional text and want to display it in TextView. Now I have to add line breaks between LTR and RTL parts of text without changing direction of text after each line break.
It is obvious that using \n creates new paragraphs so direction of each new paragraph will be determined by its content and will be not typeset with direction of whole of the text.
In LibreOffice Writer we can use SHIFT+ENTER for adding line breaks instead of new paragraphs. Is this possible in Android? Note I do not want to use <br/> tag of HTML.

you can use \n where you want to break the line as below
textView.setText("Hello \n World")
or
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello \n World"
android:textSize="45dp"/>

Related

How to gives space to paragraph inside string value

I have some text inside string of value folder in android studio and I want to give space between paragraph but I don't know how to do that ?
you can use \n for new line and add space by entering space
for example:
<string name="above_eightteen">I confirm that \n I am above 18 years and agree to all Age restriction disclaimer.</string>
and use this string value in your layout.
Most probably your text will be a straight line in the string.xml file. find out the point where a paragraph is starting or ending, and put "\n" before paragraph start or after paragraph end.

Android TextView HTML text break at the end of line

In my text view, I am setting the HTML text:
mTextView.setText(Html.fromHtml("THIS USEFUL INTERESTING HTML TEXT IS DISPLAYED IN ANDROID TEXT VIEW"));
The Text is displayed as below:
The long word at the end is breaking with a hyphen. I want the complete word to go into the next line instead. How can I achieve this? I need to use HTML text only. The text can be dynamic and has to scale across various display size and orientations, I cannot put newline char in the text.
I'd suggest you to use attribute android:ellipsize to prevent breaking up a word
Try this...it should work
mTextView.setText(Html.fromHtml("THIS USEFUL" +"<br/>"+ "INTERESTING HTML TEXT IS DISPLAYED IN ANDROID TEXT VIEW"));
How to have HTML New Line Feature in Android Text View? for more clarity

TextView no wrapping special character Android

Is there a way to create/override the custom word wrap engine for Android TextView?
For example,
My name is Edwin (US:1113)
Should be displayed as
My name is Edwin
(US:1113)
Instead of
My name is Edwin (US:
1113)
Already tried changing all the possible value for the setting below but nothing good.
Android:maxLines
Android:setHorizontallyScrolling
Android:ellipsize
Android:singleLine
In my findings, word wrap in TextView does not include special characters such as :.
Updated 3.09AM 2/7/2018 UTC
As mentioned by #Umair, line breaking with "\n" is not possible because I don't know the length of the text before the colon :. This will line break the sentence too early before it reached the maximum width of the TextView.
Use 2 TextViews inside a LinearLayout with horizontal orientation and put My name is Edwin in the 1st and (US:1113) in the 2nd. If there is no space for the 2nd TextView then it will fall to the next line.

Android: Text formatting in paragraphs

I have text which is in xml and its a long text divided into several paragraphs. How shall I store this text in db and then retrieve it so that I get the text in the paragraphs form?
"Heading\n\n\tThis is the first paragraph and you can see the second paragraph below.\n\n\tThis is the second"
Store like this. Use \n\n\t before starting each paragraph. \n is for new line and \t for tab.

Numbers and hebrew text cause unwanted new lines

For some unknown reason, if I put the following string:
15 קמ
in a text view, it results in a new line between the "15" and the "קמ":
15
קמ
If I replace the "קמ" with "km" then it works fine...
Note: this doesn't happen if both string's parts are Hebrew.
Any clues?
I think that hebrew is interpreted RTL (right to left) while the other part is LTR (left to right). Given this, the TextView has to represent something like this:
\LTR 15 \RTL קמ
My logical guess is that the TextView puts the RTL part on a new line to deal with the nonsense of having both parts on the same line. If you force it all the way RTL when the locale is hebrew, I think it would regulate the rendering and solve the issue. I would suggest you try adding the "RIGHT-TO-LEFT MARK" character \u200F at the beginning of the string:
String text="15 קמ";
if (hebrew) {
text="\u200F"+text;
}
Also, it seems that some fonts are showing a graphical interpretation of the special character (while it shouldn't). You will probably need to use this font to get rid of it.
I have also experienced TextView alignment issue when I'm working with alphanumeric + arabic text in same text view, they will realign all the texts in left to right order which results ridiculous output. This kind of language issue is not really something we can fix in direct approach.
What we did were separating out the texts to more text views if applicable, which in your case I would suggest separating out the value and Hebrew text into separate TextView.
Try to use UTF-8 encoding for hebrew text, for eg:
String text = "15 קמ";
text = new String(text.getBytes(), "UTF-8");
If your textview can fit in one line, I think I might have a solution. This is the xml layout that did the trick.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="none"
android:text="15 קמ"
android:maxLines="1"
android:padding="8dp"
android:singleLine="true" />

Categories

Resources