Numbers and hebrew text cause unwanted new lines - android

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" />

Related

Hyphens are not added at line endings to join words in some languages

I have the following TextView within a ConstraintLayout:
<TextView
android:id="#+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="3"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
... />
When the text is too long to fit in one line, it will break into two lines. However, if the system language is set to Swedish it will break in the middle of words without adding a hyphen, like so:
Some text that is br
oken into two lines
I'm not sure if this is an accurate example, but you get the idea.
What I want is this:
Some text that is br-
oken into two lines
It does work when the system language is set to English. But it should also work for Swedish.
I have tried all possible combinations of android:breakStrategy="..." and android:hyphenationFrequency="..." on the TextView.
compileSdkVersion 28
targetSdkVersion 28
minSdkVersion 28
I think that the problem comes from localization and characters encoding in different languages.
I suppose you are using the translation editor to support localization. Try getting the string of each textView (etc...) and converting it to utf-8 format using this:
URLEncoder.encode(your string, "utf-8");
Then set the textView text to this string:
textView.setText(your string);
Hyphens are only added to connect words in line breaks if the language is set to English. This is probably a bug. Source: https://issuetracker.google.com/issues/37087785 https://issuetracker.google.com/issues/37010641 https://issuetracker.google.com/issues/79936443
Not every languages is available for hyphenation in every Android version. E.g. Swedish and Polish are not available in Android API 33; they should be with the following Android release.
To perform proper hyphenation Android needs the language "dictionary" which are stored here: https://cs.android.com/android/platform/superproject/+/master:external/hyphenation-patterns/;l=1?q=hyphenation-&sq=
When it doesn't have the language dictionary available, it tries to break as best as it can, based on the available space.

Character spacing for "1" in a textview

I'm using a Digital-7 Typeface for a pair of TextViews. The problem with the TextViews is that whenever I use the character "1" in it, the characters left of the "1" get pushed back and they lose their proper spacing. As seen here:
You can see the 9's are properly in place while the 1's get squished together. Is there any piece of code that can help me fix this or do I need a better Typeface?
As what Der Golem said, switching to a monospace Typeface fixed my issue.
Please try this property in XML, Hope it will work.
android:textScaleX="1.2"
Must be a floating point value, such as "1.2". You can change the value depending on your requirement.

How to make the text direction from right to left

I want to write text in (right to left language i.e. Arabic) in a TextView. But I want to make the text writing direction from right to left. gravity:rightwill align the text to right only. I want to justify the text from right to left ( to make the words and numbers appear in he entered order in the line ) . how ?
Another clever way which can be used in older versions of android and can be used anywhere in string so it's handier even in latest version of android, is to include the right-to-left mark or even left-to-right mark in the string when it's needed:
left-to-right mark: ‎ or ‎ (U+200E)
right-to-left mark: ‏ or ‏ (U+200F)
This is how it's down:
String rtl = "Hello \u200F(سلام)";
The good thing about this method is you can even use it after punctuation like (,{,[,! which are positioned right incorrectly! examples:
سلام! // wrong position
سلام!‏ // right position by adding RLM after !
look how ! is positioned correctly in second line, actually there is an RLM after it, and you can copy paste it even if it's invisible.
The second good thing as you saw it is that you can use this method (these char codes) in any system that supports RTL like browsers, word and so on.
Just put this property in your TextView and everything will workout just fine it will automatically detect your text and change accordingly
android:textDirection="anyRtl"
Too late , but the android:gravity="right" worked.
set this line in xml for textview :
android:textDirection="locale"
Try using
textview.setTextDirection(View.TEXT_DIRECTION_RTL);
or
textview.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
In case of normal edit text this will work
android:textDirection="rtl"
But in case of password fields this is not enough then follow this;
android:textDirection="rtl"
android:gravity="right"
Use android:layoutDirection="rtl" and android:textAlignment="viewStart" to make the text view right-to-left.
try this as it worked for me android:textDirection="rtl"
Add these to your editText:
android:gravity="right"
android:textDirection="rtl"
PS: android:textDirection requires API level 17
just adding my own experience with such issues. in my case in addition to suggested solutions i also had to replace English " character with the RTL (in my case hebrew) representation of such character ״
By using attributes as below in my xml code strangely I got a right to left text with correct punctuation. But this is not a perfect solution for multi language text:
android:textDirection="ltr"
android:gravity="right"
setting ltr corrects punctuation problems like having dot on right side instead of left.
gravity attribute makes text flow from right to left.
best way textDirection for TextView, Don't use layoutDirection
<TextView
android:id="#+id/viewCountTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textDirection="anyRtl"
android:gravity="center"/>

TextView ellipsize and broken symbols [duplicate]

This question already has answers here:
android ellipsize multiline textview
(23 answers)
Closed 8 months ago.
I have some problem with standard android ellipsize mechanism.
My textview xml layout is next:
<TextView
android:id="#+id/something"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/message_test_text"
android:lines="2"
android:ellipsize="end"
android:textColor="#drawable/dialogs_text_selector"
/>
Then in code I'm setting Helvetica typeface to this field. And then, in the end of second line I see broken symbol after dots:
Because it used in list, I see a list of squares.
Can I remove it without tons of code?
Thank you!
There is a issue logged here. Many people have some or the other problem when using TextView with ellipsize in a ListView. There are many workarounds that suggest changing the textview to single line, but you cant use that.
Try setting the android:maxLines="2" attribute.
Update:
Use TextUtils.ellipsize API to manually ellipsize the string before setting it to the textview. See whether the ellipsized string returned by this API contains the square char at the end.
Usage sample code:
TextPaint tp = new TextPaint();
tp.setTextSize(float textSize);
tp.setTypeface(Typeface typeface);
Charsequence elipText;
elipText = TextUtils.ellipsize ( text, tp, avail, TextUtils.TruncateAt.END);
textview.setText(elipText);
The problem here I don't think it was your code. The problem is your .tff file which is missing the ellipsize character, hence the square is displayed. Your font was not... let's say mobile optimized.

Specify number of characters in each line in TextView?

Hey, Is it possible to specify a textview to have 25 characters in a line? I have tried android:maxLength="25" though it shows the first part of the text and the other part disappears.. Thanks
Edit1: I can also put the text into an EditText.. but I want that the characters that are after 25 chars, go to a new line..
http://developer.android.com/reference/android/widget/TextView.html#attr_android:maxLength
I am afraid, maxLength works for input EditText only. :-(
yes , sure android:maxLength="5" works for TextView to set the maximum number of characters for TextView. You can set android:layout_width="130dip" for the TextView too. That will show the character in the format abc... if the text exceed the layout width.

Categories

Resources