1

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


< Marshmallow device [Kitkat to be specific but tested in Lollipop too]


Lower than Marshmallow

Charuක
  • 12,953
  • 5
  • 50
  • 88
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
  • post screenshots if possible – Shrenik Shah Mar 01 '17 at 06:02
  • @Guruprasad Rao *the ellipsize breaks down after space in text and displays ellipses* are you sure just and `F (space) Name` and see if it goes like `F...` I don't think so – Charuක Mar 01 '17 at 06:07
  • Its depend on space in TextView and TextSize with respect t your Device resolution . – Chetan Joshi Mar 01 '17 at 06:08
  • check this answer http://stackoverflow.com/questions/32860815/how-to-define-dimens-xml-for-every-different-screen-size-in-android/32861248#32861248 – IntelliJ Amiya Mar 01 '17 at 06:11
  • @Guruprasad Rao well to get over with this thing you might wish to have something like this , keep N number of chars anyway and after that ... like you did, for any screen using auto resize text view how about that ? http://stackoverflow.com/questions/41981272/textview-getting-cut-in-some-resolutions your issue is something related with the device screen size,dpi i guess, not with the OS version – Charuක Mar 01 '17 at 08:36
  • @Charuක Yes, it is kind of both.. `sdp` would much fit here.. Let me check how I can get work-around for this.. But am still wondering why it breaks for long character words and why doesn't behave like Marshmallow.. Anyways.. Thanks for your time and suggestion buddy.. :) – Guruprasad J Rao Mar 01 '17 at 09:53

1 Answers1

7

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!

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • Buddy.. You are awesome.. This simple thing was blocking me all the day.. I don't know why they don't specify this simple thing when they can identify, what's the minimum version selected for application.. Thank you, Thank you and Thank you so much.. Happy coding.. :) – Guruprasad J Rao Mar 01 '17 at 11:41
  • 1
    @Guruprasad Rao happy to help :) – Charuක Mar 01 '17 at 11:42
  • 1
    Thanks much once again.. I just can't thank you enough.. :) You don't know how huge headache was this for me.. :) – Guruprasad J Rao Mar 01 '17 at 11:44
  • Thanks mate, this solved my issue, where ellipsize didn't work when talkback was activated on Android 4.4! – Phil Mar 04 '19 at 08:18