Android setting with TextView for Hebrew text?
Asked Answered
N

2

9

I am setting text in TextView from string resource. Normally, Hebrew works in Right-To-Left format. When I set a text, it sets the text Right-To-Left format in LG, Samsung, Sony Phone but in HTC it does not work. It works in Left-To-Right format in HTC. Even I set Gravity to the TextView in Java file.

Text in TextView should be span according to the screen size. For example if it is 320 x 480 then it display in 4 lines but if it is Galaxy Tab then there may be 2 lines.

Here is my Code snippet:

In Java:

private TextView mVersionInfo, mVersionDescriptionOne, mVersionDescriptionTwo, mVersionDescriptionThree;

mVersionInfo = (TextView)findViewById(R.id.VersionInfo);
mVersionDescriptionOne = (TextView)findViewById(R.id.VersionDesc1);
mVersionDescriptionTwo = (TextView)findViewById(R.id.VersionDesc2);
mVersionDescriptionThree = (TextView)findViewById(R.id.VersionDesc3);

mVersionDescriptionOne.setGravity(Gravity.RIGHT);
mVersionDescriptionTwo.setGravity(Gravity.RIGHT);
mVersionDescriptionThree.setGravity(Gravity.RIGHT);

in XML:

<TextView android:id="@+id/VersionDesc1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="@string/versiondesc1" android:textColor="#000000"
        android:layout_marginTop="5dip" android:gravity="right"
        android:layout_alignParentRight="true" android:layout_marginRight="10dip"
        android:layout_below="@+id/Share" android:textSize="13sp"
        android:layout_alignRight="@+id/Body" />
    <TextView android:id="@+id/VersionDesc2"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="@string/versiondesc2" android:textColor="#000000"
        android:layout_alignParentRight="true" android:layout_marginRight="10dip"
        android:layout_below="@+id/VersionDesc1" android:textSize="13sp"
        android:layout_marginTop="5dip" android:gravity="right"
        android:layout_alignRight="@+id/Body" />
    <TextView android:id="@+id/VersionDesc3"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="@string/versiondesc3" android:textColor="#000000"
        android:layout_alignParentRight="true" android:layout_marginRight="10dip"
        android:layout_below="@+id/VersionDesc2" android:textSize="13sp"
        android:layout_marginTop="5dip" android:gravity="right"
        android:layout_alignRight="@+id/Body" />

In String Resource:

<string name="versiondesc1">האפליקציה מתחברת לאתר הספק הסלולרי כדי להציג את מצב החשבון. לעיתים, כשאתר הספק איננו עובד תקין לא יהיה ניתן לקבל מידע. באם אתר הספק ישתנה האפליקציה עלולה להפסיק לעבוד. במצב כזה האפליקציה תחזור לעבודה תקינה מיד לאחר שאנו נתאים את שרת התוכנה שלנו לשינויים.</string>
<string name="versiondesc2">הערה: אנחנו לא מייצגים את חברות הסלולר ולא נמצאים איתן בקשר מסוג כלשהו!</string>
<string name="versiondesc3">אם נתקלת בבעיה, השתמש/י בכפתור יצירת קשר על מנת שנוכל לפתור אותה. נשמח לקבל כל משוב על האפליקציה.</string>

What is wrong with my code?

Anybody who has worked with another language, Please guide me here.

Thanks.

Nady answered 10/6, 2011 at 5:11 Comment(4)
@niky Please review the image uploaded by me. It sets the Hebrew text but leaves a slightly visible mark at end of the text. Thanks.Nady
The bugto and done images are not available :(Lorenelorens
@kilaka Server data has been cleared. Sorry for the inconvenience.Nady
did you managed to show hebrew in your textView without any 3rd party lib ? (showing unicode in textview)Petrick
M
32

Gravity will only affect alignment and will not set base direction for the text. That it works on some devices and not others may be a font issue, or perhaps an OS version issue. Try adding a RIGHT-TO-LEFT MARK character (\u200F) at the start of your text. This might help the display on an HTC and will not hurt anything on devices where it is already working.

Messroom answered 10/6, 2011 at 5:24 Comment(24)
@Hello Ted, I have not worked with RIGHT-TO-LEFT MARK character (\u200F). Would you please give me some snippet? so that I can figure out how to set it. Thanks.Nady
@AndroidVogue - Android's resource parser, unfortunately, is not XML-compliant, otherwise it would be a simple matter to insert the character as an escape sequence at the start of the text. But since that doesn't work, the easiest thing would be to type or paste the character directly into the resource file. Eclipse ought to correctly save it as UTF-8. (In Windows, you can use Character Map to copy 200F to the clipboard. It's under the Unicode subrange General Punctuation.) If that doesn't work, the only thing I can suggest is prepending the character in code.Messroom
@Ted, I have pasted all Hebrew text in resource file as you can see in the code I provided but in HTC is does not display correctly from Right-TO-Left format as it does work with LG, Samsung, Sony...Nady
I saw that. I'm suggesting that you edit the resource file and put a RTL mark at the start of each resource string. I don't hold high hopes for this, but others have posted (see here) that this may help. There may also be a font issue on the HTC. What version of the OS is the device running?Messroom
@Ted, So I have to put something like this: <string name="versiondesc2">(\u200F) הערה: אנחנו לא מייצגים את חברות הסלולר ולא נמצאים איתן בקשר מסוג כלשהו!</string> Did I put correctly?Nady
@Android - That's not what you should be seeing in the resource editor. The U+200F character should be the first character in the text and should not have parentheses around it. (Since the base flow of text in the resource editor is left-to-right, I think it should show up--if visible at all--between the exclamation mark and the het of חברות. If not visible, you should be able to tell it is there by using the left/right arrow keys; the cursor should not move when you traverse over the 200F. Because of the flow, it's sometimes hard to know whether you are at the start or end of the line.)Messroom
@Ted, Sorry but did not work with like this so not getting the exact <string name="versiondesc2">\u200Fהערה: אנחנו לא מייצגים את חברות הסלולר ולא נמצאים איתן בקשר מסוג כלשהו!</string>. May be like this...!!!Nady
Hm. Is the string \u200F showing up on the screen? If so, then the character itself isn't getting into the resource file. The easiest way to get the actual character in is to copy it to the clipboard from Character Map and then paste. I'll also reiterate my earlier caveat: I don't hold a lot of hope for this approach; it's just something I read about somewhere that might help. If you can't get it right, don't spend a lot of time on it.Messroom
@Ted, Yes it worked but at end of string it put light mark something like !. Thanks a lot for that. How to remove that light mark from there any idea?. Thanks.Nady
Does a space character work? If not, I would suggest using a Unicode LTR mark (U+200E) instead of a visible character.Messroom
@Ted, Yes \u200F works but it leaves light visible mark at end of the string. Any idea to remove that? Thanks.Nady
@AndroidVogue - Hm. That's supposed to be an invisible mark. I don't know what else to try. You might experiment with some other supposedly invisible characters (like a no-break space--\u00A0) to see what effect they have in place of \u200F. Most space-like characters, however, are not strongly left-to-right, which I think is what you need to get the glyph ordering to come out right.Messroom
@Ted, So I have to put this invisible character at start of the string or at end? Thanks.Nady
@Ted No it does not work. I have attached an image in my question. Please have a look. It leaves a weird character at end slightly visible. ThanksNady
I think that's the RTL mark displaying, which is a font issue. (You can confirm that by using a resource that consists only of the RTL mark.) One possibility is to use the Ezra SIL SR font instead of the platform-default font. It's free and the license is quite liberal; you can download it here. Then package it with your app and use it as described in this answer.Messroom
@Ted, It worked. Please see the attached image. Thanks a lot. Thank your very much. This font looks like ancient one, would you please suggest me any modern font which renders Hebrew as Ezra SIL SR font does. Thanks.Nady
@AndroidVogue You can try the DejaVu fonts. They are also free and have a liberal license. Although their Hebrew support is incomplete, they should be perfectly fine for what you are doing (consonants and the occasional vowel in regular type face).Messroom
@Ted Hopp Completely worked. Thanks a lot Ted for giving me such important lessons about Hebrew. You guided me so very well so that I can finish this task. Without your help, it would not possible. I owe your help. Thank you very much. I hope in future you will help people like me. I recommend everyone, this user named Ted Hopp is very kind and he works like working on his own problem. whatever he gives suggestion, just follow it and there is a proper solution of your problem. Great man I have ever seen in my life.Nady
please help me with this. I've tried using the dejaVu font (DejaVuSans.ttf) , and i've tried adding '\u200F' to the beginning (and also other variants) of the text, but it doesn't work. what should be the correct solution for this?Paramecium
@androiddeveloper - "it doesn't work" isn't very specific. What's going wrong? Is any part of it working?Messroom
@TedHopp it doesn't work, since the Hebrew text isn't aligned to the right, and the punctuations (signs like "." and ",") aren't in the correct place . i've created a whole new question about this problem here: #15746591Paramecium
@TedHopp Thanks for helping, is it possible to add the char in a custom TextView for all the texts in the application? Like so: StringBuilder stringBuilder = new StringBuilder(getText()); stringBuilder.insert(0, '\u200F'); setText(stringBuilder.toString());Sideslip
@TedHopp Do you know of an android library that adds support for Hebrew on API 8 platforms and higher? :) Wishful thinkingSideslip
@Benny - I don't know of any way to add \u200F to all text views, other than doing it explicitly like you suggested. I also don't know of any Hebrew support libraries. KitKat (API level 19) has pretty good Hebrew support (although a few things are still lacking, such as TextView scroll bars automatically being on the left for right-to-left text).Messroom
C
3

here is an example from my hebrew string xml, Thanks to Ted Hopp's answer:

you need to add '\u200e' before the char that causes you the problem:

<string name="basic_text1">המר על תוצאת המשחק\u200e:</string>

and the result will be:

 :המר על תוצאת המשחק
Crinose answered 19/3, 2015 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.