Android LinearLayout to contain multiple TextView with ellipsize
When displaying a ‘name’, there is always a must-handle case:
A very long long long name.
You probably already know ellipsize to automatically replace the exceeding text with three dots …
Everything works great when there is nothing appending to that TextView.
Imagine this design, we have a contact ‘group’, which can be built with several single contacts. And we want to show a tag ‘In group’ to indicate that I am one of the members of this group. According that, we will have to display that tag right after the group name.
You may try adding a TextView appending to that name.
Unfortunately that won’t work. The ‘wrap_content’ of the contact name will take over all remaining width, and you won’t see the contact tag.
This seems to be a bug to me. But still, I have to figure out any alternative to achieve the desired layout. After quite surfing and try&error, it has to apply ‘android:layout_weight’ to solve this problem.
The contact name has to use android:layout_weight=”1" to take over the remaining width excluded the width of contact tag. Then the contact tag has to use android:layout_weight=”0" to declare that it won’t be competing the width with contact name, it will just use a fixed width to wrap its content.
You might feel confused when the contact name with android:layout_weight=”1" , which means it could take over the whole remaining width even its name text is not that long.
That totally confused me too!
However the result shows me
android:layout_weight=”1" only guaranteed that when contact name ‘stretched’ it width, it must not take over contact tag’s width, because contact tag has declared itself android:layout_weight=”0", a fixed width layout.
I know it’s hard to understand and probably is another bug of android layout. (Lol), or maybe I interpret this in the wrong direction.
After all, this is the trick.
Feel free to share me any feedback, thanks!