Using ellipsis in a TextView and stretching the view ONLY if necessary

I have 2 TextViews per row and 2 requirements:

1) If the first TextView is not too wide, it should look like this

| [1 text] [2 texts] |

2) If the first TextView is too wide, it should look like this

| [1 text of tex text ...] [2 text] |

The second requirement is simple, you can use android: layout_weight = "1", for example:

<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" android:ellipsize="end" android:text="1 text text text text text" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2 text" /> </LinearLayout> 

but if the first TextView contains short text, it looks like

| [1 text; ; ]]]

which is unacceptable.

So, how to satisfy simultaneously 1) and 2) requirements?

+4
source share
2 answers

In the meantime, I found a very simple solution: just set the LinearLayout width to "wrap_content" instead of "fill_parent".

 <LinearLayout android:orientation="horizontal" android:layout_width="WRAP_CONTENT" android:layout_height="wrap_content" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="true" android:ellipsize="end" android:text="1 text text text text text" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2 text" /> </LinearLayout> 
+7
source

I do not think you can do this using just the layout. For Android to ellipse Text1, it needs to know the exact width of the TextView . You can only do this by specifying a fixed size or by providing fixed sizes to other views. You do not want to do this.

You need to measure the width of the text in each TextView , as if it were being displayed . Once you know the width that each text will take, you can then make decisions in the code on how to get the layout to do what you want.

Add another View to LinearLayout with android:layout_weight="1000" . This will take up all unused space if the width of text1 and text2 does not exceed the width of the screen. Now calculate the width of text1 and the width of text2 as follows:

 Rect bounds = new Rect(); Paint textPaint = textView1.getPaint(); textPaint.getTextBounds(text1, 0, text1.length(), bounds); int widthText1 = bounds.width(); textPaint = textView2.getPaint(); textPaint.getTextBounds(text2, 0, text2.length(), bounds); int widthText2 = bounds.width(); 

Now you know the width that text1 and text2 would need if they were fully displayed.

 if (widthText1 + widthText2 > screenWidth) { View3.setVisibility(View.GONE); // Don't need View3 as there is no extra space } 

In one case, View3 will take up all the remaining space. Otherwise, TextView1 should be ellipsed at the end.

I have not actually tested this, so don't be too heavy if this doesn't work.

+1
source

Source: https://habr.com/ru/post/1481036/


All Articles