I'm trying to get a row of text which will be something like
foofoofoo - barbarbar
but I want it to ellipse foo and bar if it won't fit on one line. i.e. I'm trying to shorten them down.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="0dip"
android:layout_weight="1"
android:textColor="#ffffff"
android:singleLine="true"
android:ellipsize="true"
android:text="foofoofoofoo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text=" - " />
<TextView
android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="0dip"
android:layout_weight="1"
android:textColor="#ffffff"
android:singleLine="true"
android:ellipsize="true"
android:text="barbarbarbar" />
</LinearLayout>
Bear with me this works partially.
Setting the TextView
's layout_width
to be 0dip
and layout_weight
to be 1
means they will take up 50% of the available space each.
Setting the singleLine
to true
and ellipsize
to true
means they will look like foofoo... if the text is larger than the container.
Therefore my outcome (if the text is longer) should be
foofoo.. - barbar..
Which it is! So this works.
Now the case I'm trying to fix is, if the first TextView
(id:text_1) has text that is less than the 50% given by layout_width="0dip"
and layout_weight="1"
I want it to wrap_content
. Otherwise it looks like:
foo blank space - barbar..
and I want
foo - barbarbar
This is why I have changed layout_width="wrap_content"
and added android:maxWidth="0dip"
but this doesn't work! It seems to be ignoring me saying wrap_content
and still giving this view 50%!
I read that you need to add android:adjustViewBounds="true"
for maxWidth
to work but this had no affect.
This is the best I could do, try changing the strings to see if it works as intended.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shrinkColumns="0,2"
android:stretchColumns="0,2">
<TableRow>
<TextView android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="foofoofoo"/>
<TextView android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:gravity="center_horizontal"/>
<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="barbarbarbarbarbarbarbarbarbarbarbar"/>
</TableRow>
</TableLayout>
It is rather hard to tell exactly what you want, but I believe the answer is you can't do this unless you dynamically adjust your layout in code. The reason for this is because you are asking the system to work in multiple different ways.
First, you want the views to share the space equally when the amount of text in the view is the same.
Second, you want the views to NOT share the space equally when there is less text in the other view.
There are multiple other cases here that can be handled any way you want. What I'm trying to convey however, is that your asking the same layout to handle things in different ways which is why you aren't able to achieve this with a single layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With