text ellipsize behavior in android version < 6.0 - android

I have below layout in my xml which will contain 3 TextView and they are aligned left, centre and right respectively.
<RelativeLayout
android:id="#+id/relRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="#string/contact"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/colon"
android:id="#+id/colonRow1"/>
<TextView
android:id="#+id/txtPrimaryContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_toEndOf="#+id/colonRow3"
android:layout_toRightOf="#+id/colonRow3"
android:ellipsize="end"
android:gravity="end"
android:maxLines="1"
android:text="Joe SmithMartinWilliamCathcsdsdsdadas"/>
</RelativeLayout>
The problem I am facing now is that, the ellipsize doesn't go well or work as expected with android device version < 6.0. I mean for device with Marshmallow version, the ellipsize is breaking the text once max width is reached, but for devices having lollipop or kitkat version, may be lower too, the ellipsize breaks down after space in text and displays ellipses, it does not break for long text.
Here is how it looks in android version less than Marshmallow:
Consider the name is FName LName
Contact : FName...
Here is how it looks in android version greater than Marshmallow:
Contact : FName LNa...
Is this a bug in lower versions or expected behavior. Or is there any other way to do this? I would like to have the Marshmallowversion text in all the devices. Anyway I can achieve this?
Update
Turns out, my assumption was wrong after #Charu's comment. It actually hides out for long text in device version < Marshmallow. How can I come over this?
Right now I've been using below code, but that doesn't scale up when we check with different devices with different screen size. I've attached screenshots for reference.
if(android.os.Build.VERSION.SDK_INT<Build.VERSION_CODES.M && name.length()>20)
name=name.substring(0,20)+"...";
else
txtPrimaryContact.setEllipsize(TextUtils.TruncateAt.END);
>=Marshmallow device
< Marshmallow device [Kitkat to be specific but tested in Lollipop too]

You might tell me that android:singleLine="true" is kind of deprecated that's why you used android:maxLines="1" but trust me adding this android:singleLine="true" to your textView should fix the issue!

Related

How do I scale my text size for multi-device compatibility on Android Studio?

I am having trouble scaling my text size based on the user's device. I use constraint layout to constrain the TextViews. I found that autoTextSize could be used to automatically fit my text in the TextViews. However, when I run my code on an Emulator like BlueStacks, some texts are not scaled properly. Weirdly, some work but some do not, even though I am implementing it the same way.
This is an example of what it looks like: The register button is in a big font but other texts aren't
This is what it looks like on my layout Editor:
My code for the register button:
<TextView
android:id="#+id/register"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="REGISTER"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="5:1"
app:layout_constraintEnd_toEndOf="#id/guideline3"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#id/guideline2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />
my code for the "up to 8 characters" text.
<TextView
android:id="#+id/req"
android:layout_width="0dp"
android:layout_height="0dp"
android:autoSizeTextType="uniform"
android:text="*up to 8 characters"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="7:1"
app:layout_constraintEnd_toEndOf="#+id/guideline5"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/guideline4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.538" />
Am I misusing autoSizeTextType somehow?
Also, my "Enter Username" text is very small. I use EditText and implement it the same way I did for the other two texts. If there is anyway I can make the font size bigger to fit the layout size, please let me know.
your <TextView android:id="#+id/register" is using app:autoSizeTextType="uniform" attr, <TextView android:id="#+id/req" have android:autoSizeTextType="uniform" - note the difference app/android. try to consolidate this param, you should probably use app in both/all TextViews
note that setAutoSizeTextTypeWithDefaults was introduced in API 26, so when you use android: prefix then this attr will be respected only on 26+. using custom app: makes this attr usable for TextView (in fact TextViewCompat) on any OS version
in Android Studio above layout preview you have an option to switch "Device to preview" and also API version - try to change to some older (in this sample 25 at most, preferably lowest supported minSdkVersion) or newest OS version, IDE should catch and show differences in look
Use TextView params like this
android:autoSizeTextType="uniform"
android:autoSizeMinTextSize="12sp"
android:autoSizeMaxTextSize="100sp"
android:autoSizeStepGranularity="2sp"
You can check the official documentation for more info.

android:ellipsize not working as intended

here is some code, I can replicate the bug even if I strip everything down to a simple TextView.
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:singleLine="true"
android:text="I AM VERY SMART I AM VERY SMART I AM VERY SMART"
android:textSize="16sp">
</TextView>
I wish I could post a screenshot but SE is not letting me. what happens is that the text is truncated in the middle (as intended) but with a single dot (.) rather than a full ellipsis (...)
it does this on all lollipop tablets that I own (nexus 7 and nexus 9). other devices are displaying the ellipsis properly. I have researched the issue and found various comments about people getting the same problem, but no answers.
I have the same problem as same as you.
I achieved my goal by setting:
width="85dp"
maxWidth="85dp" // same as above
maxLines="1"
Try this:
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:singleLine="true"
abdroid:focusable="true"
android:focusableInTouchMode="true"
android:text="I AM VERY SMART I AM VERY SMART I AM VERY SMART"
android:textSize="16sp">
</TextView>

Android RelativeLayout: layout_above appears to behave incorrectly with layout_alignBaseline

In Android I am trying to do a RelativeLayout in which I want to use layout_above on something that is aligned with layout_alignBaseline, but find that this is not working correctly.
Note: I tested with API levels 14, 15, and am going to check in 19 (latest) as soon as I get that installed, with the same result. By the way, I'm referring to appearance in Eclipse layout editing GUI - I haven't tested on a real device, but I assume it's supposed to be consistent...
My real layout is complicated but I have reduced it to this simple example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="A"/>
<TextView
android:id="#+id/b"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/a"
android:text="B"/>
<TextView
android:id="#+id/c"
android:layout_alignParentLeft="true"
android:layout_above="#id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"/>
</RelativeLayout>
With this layout, I would expect to see the letter A at bottom right (yes), the letter B at bottom left (yes), and the letter C immediately above the letter B. In fact, the letter C does not appear - it seems to have been positioned off the top of the view.
If you replace the layout_alignBaseline on B with layout_alignTop or alignBottom, because in this example A has the same font size, this does not change the appearance of A and B at all - but now C appears in the correct place.
My basic question is, why does this not work when using the baseline alignment? Is this a bug or am I doing something wrong?
Notes:
Obviously in this simplified example there is no reason to use layout_alignBaseline, but it is necessary in cases where the font sizes are not the same.
This is not an urgent problem because I have come up with a less neat way to achieve a similar layout. But I'd like to know for future reference why it doesn't work.
I did look at the related questions but didn't spot the answer.

In Android 2.3.x text is not showing in two lines

Here is my code :
<TextView
android:id="#+id/text_view_title"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_margin="5dp"
android:ellipsize="end"
android:maxLines="2"
android:textSize="#dimen/text_size_small" />
In Android version 2.3.2, text is not showing in two lines, while in higher version device the text is showing in 2 lines. Also when I remove the tag ellipsize then text will be shown in 2 lines. How can I solve this issue?
Remove the ellipsize tag. If you really want it for higher versions, add it programatically when you inflate the layout for android versions above 2.3.
Alternatively, use different layouts for 2.3 and before vs 3.0 and later. But that's more likely to lead to maintenance bugs. If this is the only difference I wouldn't go that route.

Ellipsize attribute not working on Android 4.0

I'm currently testing an app on Android 4.0 Ice Cream Sandwich before releasing an update to the market. During the tests I realized that the ellipsize attribute stopped working.
I used it on a list view to truncate item titles which were to long. On Android 2.3.7 everything renders as expected. Titles that are too long get truncated and three dots are append to the end of the string. On Android 4.0 the title gets just truncated after the last word that fits completely on the screen, the rest gets skipped.
I've testes it on a Android 2.3.7 and a Android 4.0.3 phone. This behavior can even be tested in eclipse by changing the Android version in the Graphical Layout editor.
Does anyone has the same problem. Any work a rounds for this?
android:singleLine="true" works for me.
enter code here <TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:lines="1"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textColor="#ff4500"
android:text="Om Namah Shivaya : Om Namah Shivaya : Om Namah Shivaya : Om Namah Shivaya : Om Namah Shivaya" android:layout_marginTop="2dp">
<requestFocus
android:duplicateParentState="true"
android:focusable="true"
android:focusableInTouchMode="true" />
I can recall I had a similar issue and I could resolve it by setting the TextView(which had ellipsize attribute) gravity to left. But most probably this is another issue.

Categories

Resources