RTL alignment not working with long text - android

I'm working on a layout that includes both English and Hebrew (intended as right-to-left) text, in separate views. When a line of Hebrew text gets beyond a certain length, it becomes written left-to-right (I assume that this has to do with length because it looks fine with shorter text, and when I display it on a tablet instead of a phone).
The relevant view looks like this:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="center"
android:textDirection="rtl"
android:text="#string/tx_middle_text"
android:textSize="#dimen/big_text_dp" />
and the string is defined in strings.xml like this:
<string name="tx_middle_text">טקסט בעברית</string>
(I've replaced the original text, which was 50 characters long, and made up entirely of Hebrew letters and white-spaces).
Note the text has the rtl attribute. I've tried replacing it with anyRtl, and I've tried changing gravity to "right" - neither helps.
I need the text to remain in one line and be cut off with an ellipsis if it doesn't fit - as it is, that's what happens, but with the text written left-to-right.
How can I fix this?
Edit: For an ad-hoc solution I made a shorter string as an alternative resource for the smaller layout (it works as long as the text is less than one line long on a given device), but I'd still like to know if there's a general solution to this.

RTL is detected automatically by the system depending on the characters. i.e for Arabic characters, text will be drawn from right to left.
This is an example on how my spinner is drawn. For this TextView:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_item"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="15sp"
android:gravity="center_vertical|start"
android:textColor="#color/spinner_filter_text"
android:ellipsize="end"
android:singleLine="true"
android:paddingStart="20dp"
android:paddingEnd="20dp"/>
And these strings:
<string name="all_categories">All Categories</string> (values)
(values-ar)
The result is:
If I modify the TextView with:
android:layout_width="100dp"
The result is:
Which I think is fine... Note that in strings.xml the string direction for ar language is wrong but Android is drawing it properly. I guess it is because of some Android Studio setting.

Replace android:singleLine="true" with
android:lines="1"
android:maxLines="1"
also add android:ellipsize="end"

Related

Android Text in different language take different height

We are showing some text in our application (say in a RecyclerView with GridLayoutManager). This text is shown in different languages as highlighted in the image below. Due to this, the height of TextView is different as different language fonts are taking different spacing.
Please refer to the sample image below and the required code to draw the text view.
<com.google.android.material.textview.MaterialTextView
android:id="#+id/tv_title_one"
android:layout_width="0dp"
app:layout_constraintWidth_percent="0.5"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:fontFamily="#font/lato_bold"
android:gravity="start"
android:maxLines="3"
android:minLines="1"
android:singleLine="false"
android:includeFontPadding="false"
android:textColor="#541388"
android:letterSpacing="0"
android:lineSpacingExtra="0dp"
android:textSize="32sp"
android:textStyle="normal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Class 12th (Biology) 2021 - 2022" />
Dummy layout with two similar text views but with different language
Please let me know if there is anything missing in the problem description. Thanks for your valuable feedback.
Update #1:
Since there is no direct answer to it. We are going with making card height and text height-restricted as mentioned in the accepted answer.
I just think that the reason could come from the font you are trying to use for rending text in this TV. For every language, the character size maybe vary a bit from width to height due to the language specs.
Could you try to use the default system font to check if you still have that spacing issue?
Or another suggestion would be:
Use autoSizeMaxTextSize, autoSizeMinTextSize feature to allow text size being automatically resize to fix the textview when possible.
Apply a fix Height for your text content with extra Vertical Space, and add android:ellipsize as your wish.
Still wonder:
Is your app displaying multiple language at a same time on UI?

TextView text multiple lines for Android v4.1+

I have a TextView in Android with a Text that can vary quite a bit in length. It could be a single word, it could also be more than ten full sentences.
My TextView has android:layout_width="match_parent" and android:layout_height="wrap_content". I've looked around and found a lot of ways to make it wrap over multiple lines (some requiring multiple of these):
android:scrollHorizontally="false"
android:singleLine="false"
android:ellipsize="none"
android:maxLines="10"
etc.
So my question is: Which should I use to wrap my TextView-Text in Android version 4.1+?
PS: I haven't tested any of these yet, but since I've found so many different answers on SO-questions about Text-Wrapping, I was wondering what the "best" method is (for my Android version).
I ended up using:
<TextView
...
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
android:scrollHorizontally="false"
android:ellipsize="end"
android:maxLines="10" />
When I looked at the singleLine in Eclipse it gave the following java-doc:
Constrains the text to a single horizontally scrolling line instead of
letting it wrap onto multiple lines, and advances focus instead of
inserting a newline when you press the enter key. * Deprecated: This
attribute is deprecated. Use "maxLines" instead to change the layout
of a static text, and use the "textMultiLine" flag in the inputType
attribute instead for editable text views (if both singleLine and
inputType are supplied, the inputType flags will override the value
of singleLine). [boolean]
So I now use maxLines="10", since I don't want an entire life-story to be inside the TextView, 10 lines should be a good maximum. I've added the ellipsize="end" to have three dots (...) at the end of the text when it has more than 10 lines. And the scrollHorizontally="false" does the trick of allowing multiple lines without a horizontal scroll-bar.
try setting this attributes:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="none"
android:singleLine="false"
here
to elipsize, a neologism, means to shorten text using an ellipsis, i.e. three dots ... or more commonly ligature …, to stand in for the omitted bits.
Say original value pf text view is aaabbbccc and its fitting inside the view
start's output will be : ...bccc
end's output will be : aaab...
middle's output will be : aa...cc
marquee's out put will be : aaabbbccc auto sliding from right to left

Android - TextView ellipsize doesn't work properly

Android ellipsize doesn't work as expected in some cases.
What I'm expecting is three dots on the last line, but in cases it adds the 3 lines plus a few characters related to the rest of the text afterward. Is it possible to change this behavior?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:singleLine="false"
android:ellipsize="end"
android:maxLines="2"
android:layout_marginBottom="16dp"/>
According to me android:ellipsize="end" depends on the words in the TextView.
If following word is lengthy then it omits the word and shows ...
If the following word is small then it will fit correctly and last fill ...
Hope it helps.

android multilingual application

I try to translate my application to other languages. I added a few local files values/strings.xml to the project with relevant translation. The text is translated now, however it is not enough. There are 2 problems:
In some languages the words are longer then in English and as a result text exceed the space. For example a few letters in the word on the button appear in second row.
I have a form with the fields 'from' and 'to'. In English they appear left to right, however in right to left languages they should appear right to left on the screen.
How I can solve those problems?
You can define other layout for right to left languages. You should place it to layout-<your language code>.
Also you can truncate and replace last chars of the string if it is too large.
You can do this using android:ellipsize and android:singleLine attributes:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_mytext"
android:ellipsize="end"
android:singleLine="true"
/>
Another way is to define text size in dimens.xml for needed languages and use it as follows:
in dimens.xml:
<dimen name="text_size">32sp</dimen>
in layout:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_mytext"
android:textSize="#dimen/text_size"
/>

Limiting a String to 2 lines in android, then cut it and concatenate a String

My question may sound a bit weird but I'll try to explain it better here.
I ahve a TextView in android, at the inferior part of my activity. I want to have it limited to 2 lines, which is easily reachable by adding the following line in the TextView xml element:
android:maxLines="2"
Okay, now we've got it limited to 2 lines.
Then, in my Activity, I make:
termsandconditions = (TextView) findViewById(R.id.textView1);
termsandconditions.setText(terms);
Okay, now I've got a big string with the terms and conditions, but limited to 2 lines due to the xml attribute.
Now my question is, how can I cut it after having it limited to 2 lines, and concatenate a string with "Read more"? I don't need it to be in the same textView or whatever, I only want that it looks like:
Terms: blablablalblalbla blal blablalblalblalblalbla lalblalblalblalblalblalb lalblalblalb lalblalblalblalblalb lalblalblalb lalblalblalblalb lalb bla View More.
Thanks and I hope you can understand my problem.
You can use setEllipsize (TextUtils.TruncateAt where) method of TextView or the android:ellipsize XML attribute.
public void setEllipsize (TextUtils.TruncateAt where) Added in API
level 1
Causes words in the text that are longer than the view is wide to be
ellipsized instead of broken in the middle. You may also want to
setSingleLine() or setHorizontallyScrolling(boolean) to constrain the
text to a single line. Use null to turn off ellipsizing. If
setMaxLines(int) has been used to set two or more lines, END and
MARQUEE* are only supported (other ellipsizing types will not do
anything).
Related XML Attributes
android:ellipsize
Might be a bit hacky, but here's my suggestion:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="your_text_view"
android:text="long long long text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="link"
android:text="View More"
android:onClick="addMoreLinesAndHideThisTextView"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Some additional tweaking may be needed, but I think you got the idea.

Categories

Resources