Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the height of a list item to that of two lines

When using a RecyclerView, is it possible to set the item height to be that of two text lines?

Suppose an item has a TextView on it. Some items might have a long text that does not fit in one line. I will restrict the maximum number of lines to 2. However, if some items have 1-line height and others have 2-line height, it would look ugly. Therefore, I want all items to have the height of 2-lines, even though the item has only one line of text.

I can set height of an item in DP, but the problem is that since I use system font settings, hard-coding a numeric value for height should be bad. Some systems may have large fonts with large margins.

Is what I am trying to do possible?

like image 491
Damn Vegetables Avatar asked Apr 11 '16 10:04

Damn Vegetables


2 Answers

have you tried: android:lines="2" in xml? according to doc: Makes the TextView be exactly this many lines tall.

like image 81
Assaf Shouval Avatar answered Nov 08 '22 13:11

Assaf Shouval


There is a option you can set minimum height of textView. android:minHeight="50dp" or you can add this android:lines="2" to your xml code.

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"                
    android:minHeight="50dp"
    android:text="Hello"
    android:textSize="20sp"
    android:lines="2"/>

Now android:layout_height="wrap_content" will manage font size.. 20sp or 50sp or any size of system font it wouldn't be a problem..

Hope it helps

like image 7
Tanim reja Avatar answered Nov 08 '22 13:11

Tanim reja