5

Right now if there is a list item that is 2 lines in height then all the rows are two lines in height

How can I make the rows' height independent on other rows.

ListView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/user_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null" />

</LinearLayout>

Row

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="1dp"
    android:paddingTop="1dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <TextView
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:maxLines="2"
        android:ellipsize="middle"
        android:textColor="@android:color/holo_blue_dark"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/points"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:gravity="right"
        android:textStyle="bold" />

</LinearLayout>

I am using a custom adapter extending BaseAdapter. the getView there is nothing special, just a viewholder patern I don't change height or anything

124697
  • 22,097
  • 68
  • 188
  • 315
  • Could you post some images of what you want to achieve? – sriramramani Aug 30 '13 at 02:41
  • @sriramramani Ahmed Ekri's answer has an image of what I want to achieve as far as the height wrapping – 124697 Aug 30 '13 at 09:48
  • @codefish578841441 if u want to create list view in which each row height is independent of each other then u need to create ur own list-view(i.e put inflated views in scroll view and handle click events on each of the raw..)..by doing this u can create raw of any height depends on ur no of text lines. – TheFlash Aug 31 '13 at 06:19
  • @codefish578841441: kindly post your customadapter code – Mehul Joisar Sep 04 '13 at 09:19

5 Answers5

10

why don't you try making the height = 0dp and weight 1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingBottom="1dp"
    android:paddingTop="1dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <TextView
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:maxLines="2"
        android:ellipsize="middle"
        android:textColor="@android:color/holo_blue_dark"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/points"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:gravity="right"
        android:textStyle="bold" />

</LinearLayout>

update:

check this answer

https://stackoverflow.com/a/7513423/1932105

enter image description here

Community
  • 1
  • 1
Ahmed Ekri
  • 4,601
  • 3
  • 23
  • 42
3

You are restricting it the TextView to have a maximum of 2 lines. It will never grow more than that. Remove android:maxLines="2". It should work automatically.

sriramramani
  • 1,088
  • 1
  • 9
  • 8
3

The trick for me was not setting the height -- but instead setting the minHeight. This must be applied to the root view of whatever layout your custom adapter is using to render each row.

Source: https://stackoverflow.com/a/7368439/2498729

android:textAppearance="?android:attr/textAppearanceLarge" 

seemed no effect.

android:minHeight="?android:attr/listPreferredItemHeight" 

changed the height for me

Source: https://stackoverflow.com/a/4274505/2498729

Community
  • 1
  • 1
2

Looks like you'll have to make visibility of your text views to be View.GONE when they are empty. You can do it in adapter implementation.

pointsView.setText(pointsText);
pointsView.setVisibility(TextUtils.isEmpty(pointsText) ? View.GONE : View.VISIBLE);

But be careful since your views are in RelativeLayout and its rules can stop working when an anchored view gets visibility View.GONE.

In such case you may try setting text size to zero instead of changing the view visibility, but I haven't tried it myself.

pointsView.setTextSize(TextUtils.isEmpty(pointsText) ? 0 : defaultSize);
Roman Mazur
  • 3,084
  • 1
  • 20
  • 24
1

If you are not limited with the TextView MaxLine then please remove android:maxLines="2" this will automatically adjust the height of list.
If you are looking for increasing the height of one particular row independent of others. After setting the adapter value for the list. Get the list
final ListView lv = getListView(); then form the lv get the particular row by
lv.getItemIdAtPosition(position) or any similar logic that best suited in your code. and change the row appearance programmatically as you looking for. Thanks.

Pradip
  • 3,189
  • 3
  • 22
  • 27