How to make the text direction from right to left - android

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

Related

Autoinsert right-to-left attributes Android Studio

I don't know all features of Android Studio, I have tried to search,but found nothing.
I wonder if it is possible to make Android Studio autoinsert attributes required for right-to-left support.
For example I have typed following line
android:layout_marginLeft="10dp"
Is it possible to make Android Studio insert marginStart attribute automatically ?
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
And vice-versa(for padding too).
This would save some time.
Maybe someone knows how to get such behavior, I will be grateful for any help. Thx.
Apparently, you can't do that. You have to write them yourself.
The difference between layout_marginLeft and layout_marginStart for example is that layout_marginLeft is executed for left to right languages like English, while `layout_marginStart' is only executed for right to left languages like Arabic.
So if you are going to add string translations to your app including arabic or any other right to left languages, you will need to write marginStart or marginEnd attributes... So anyway, if you didn't write layout_marginStart for example and your app doesn't support Arabic language or any other right to left languages, no error will occur to the user; it is just a warning that Android Studio tells you.
Definitely the same concept is applied to padding attributes. Hope that helps you.
Edit:
If you don't like to see Android Studio warning you these warnings, you can simply disable that by clicking on the yellow light bulb beside the yellow highlighted warning and selecting Edit 'Using left/right instead of start/end attributes' inspection settings, then uncheck it from the list.
But if you don't want to change the inspection settings, you can just add the following to your View that you don't want to use start/end attributes in it:
tools:ignore="RtlHardcoded"
and add that to your parent layout that contains that View:
xmlns:tools="http://schemas.android.com/tools"

Edittext input type alignment extra space

I want to give extra space to input texts in my edit text.
here is what I have now :
but I want to achieve this :
which property of edittext will do this (in xml) ?
or I have to do this in another way ?
Thanks in advance !
android:paddingLeft is the property you need. You can use it as android:paddingLeft="8dp"
Checkout this answer here: https://stackoverflow.com/a/4619943/1739882
Add this attribute:
android:paddingLeft="10dp"
padding usually used to give an alignment to the text inside the EditText or even Buttons
please inform me if this is what you are looking for

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

Android: Something better than android:ellipsize="end" to add "..." to truncated long Strings?

This property makes
"short and very-long-word"
to
"short and"
. But I want to have smth. like
"short and very-lon..."
Right now I truncate the String in Java code. However, thats based on the number of characters and not the actual length of the link. So, the result isn't very nice.
String title;
if(model.getOrganization().length() > 19) {
title = model.getText().substring(0, 15).trim() + "…";
} else {
title = model.getText();
}
((TextView) findViewById(R.id.TextViewTitle)).setText(title);
Update
Just noticed, this property actually adds "..." in a few cases. But not in all of them:
12345678901234567890 becomes
"12345678901234..."
However,
"1234567890 1234567890"
becomes "1234567890" and not
"1234567890 123..."
Update 2
Now it really gets funky! I just set singleLine=true and removed maxLine (The bug appears with and without setting ellipsize attribute)...
This is a screenshot take from Motorola Milestone with android 2.1 update 1. Same happens on HTC Desire with the same android version
Update 3
Now I use android:ellipsize="marquee". That seems to be the only properly working setting. It's also only moving, when focused. I see it in many other apps also. I guess its common practise.
If it's for a single line try this:
android:ellipsize="end"
android:maxLines="1"
android:scrollHorizontally="true"
android:singleLine="true"
It worked for me.
I had a similar problem and setting this property to the TextView helped:
android:singleLine="true"
Also, I was using a RelativeLayout so I limited the space the TextView could take by setting a left margin to a button to the right of the TextView, so that the text cannot expand further and is forced to truncate its content.
Maybe it's not the solution to your problem, but it helped me in a similar situation.
See my update 3. The workaround was using " marquee". I have seen many apps doing that at this time.
Now, with new versions of Android this feature is most likely fixed and will work as expected. (my guess)
Maybe you could take a look at how the private Ellipsizer class used by the OS works and modify it for your app?
Edit: Here's a working link - http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/text/Layout.java#1830

Custom Position of Hint in Edit Text box.

I was wondering if there was any way that I could get a hint at the bottom of an Edit Text view -- and then the user to start entering text at the top of the box.
As a bonus question, is there any way I can make the hint NOT disappear once the user starts entering text.
You can set the position of the text using the "gravity" attribute (as noted at http://developer.android.com/reference/android/widget/TextView.html#setGravity(int)). So to put the text at the bottom you would have;
android:gravity="bottom"
And to answer your bonus question; No, you can't display the hint when text is entered into the edit text view. Displaying the hint only when the box is empty is the defined behaviour as noted at http://developer.android.com/reference/android/widget/TextView.html#setHint(int)
(and yes, I know the links are for TextView, but EditText derives all of its' text positioning and hint handling functionality from TextView).
Actually THERE IS a way to prevent hint from hiding, and it's a cool one :-)
It gives you the Floating Label look with smooth animation very easily and it's from android itself. No extra libraries and stuff.
Try this:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Touch me and I'll fly!"/>
</android.support.design.widget.TextInputLayout>
You can change the behaviour using other xml tags like android:gravity="start|center|end" and others.
As a BONUS, you can use error messages with it :-) Here's the link to that question: https://stackoverflow.com/a/30953551/6474744
And sorry I do not have enough reputatuion to post images, so help yourself:
http://i0.wp.com/androidlift.info/wp-content/uploads/2015/09/Screenshot_2015-09-28-17-03-561.png
Enjoy :-)
The problem with using both android:gravity and android:hint is that they are inter-linked with regard to cursor position.When you position the hint using gravity and you start entering text, it is entered in the same position as the your hint which is a problem if you want it to start traditionally on the top-left corner.

Categories

Resources