Although singleLine has been deprecated for quite some time, I haven’t found a suitable replacement for certain effects. For instance, consider the following scenario:
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:text="aaaaaaa\nbbbbbbb\ncccccc" />
I want a single-line text view that truncates text with ellipsis (...) at the end.
Additionally, I need to replace newline characters (\n) with a style similar to a space.
The desired effect is illustrated in the image below:
Could you please guide me on how to achieve the same effect using other XML attributes? Note that I prefer a solution that doesn’t involve writing code, as that would defeat the original purpose.
Why do you think singleine is deprecated? There's no such notice on the TextView documentation:https://developer.android.com/reference/android/widget/TextView#setSingleLine(boolean)
But ignoring that, if you want to do it without that field- you're going to have to write code. There's no other way to do that. The easiest way is to set a TextWatcher on the field and whenever a new string is set, replace all \n in the string with a space and append an elipse to the end if one doesn't already exist (use the elipse unicode character so they can't put things between the dots).
Of course you could make a custom TextView subclass that does all that, and put that view in your xml. If you go that route, rather than a TextWatcher, override setText and getText to do that (setText to adjust the string and call super.setText(), getText() to remove the elipse again)