2

i have this ListView that i created in a Fragment. The list has dynamic content and i would like that the list fill all the screen when there are few entries. How to do that?

Here are the related XML files, myFragment.xml:

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

    <ListView
        android:id="@+id/listview"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" >
    </ListView>


</LinearLayout>

and here is listview_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" >

    <TextView
        android:id="@+id/FirstText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </TextView>

    <TextView
        android:id="@+id/SecondText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="#FFC030"    
        android:layout_weight="2">
    </TextView>

    <TextView
        android:id="@+id/ThirdText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </TextView>

    <TextView
        android:id="@+id/FourthText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </TextView>
</LinearLayout>

EDIT: In other words here is my (multicolumn) ListView in a Fragment (tabbed activity):

enter image description here

i want this result:

enter image description here

smartmouse
  • 13,912
  • 34
  • 100
  • 166

1 Answers1

1
<ListView
        android:id="@+id/listview"
        android:layout_height="match_parent"
        android:layout_width="match_parent" >
    </ListView>

set the height of the ListView to match_parent like i have done above.

UPDATED:

In your fragment class, get the height of the of the screen(programatically),then when you are setting the adapter for the ListView you need to set the height of the LinearLayout(inside listview_row.xml) to the height of the screen you got.

OR

set the height of the LinearLayout to some random value like i have set below(not recommended because the height of the screen varies with different resolution screen)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_height="500dp"
    android:layout_width="fill_parent" >

    <TextView
        android:id="@+id/FirstText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </TextView>

    <TextView
        android:id="@+id/SecondText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="#FFC030"    
        android:layout_weight="2">
    </TextView>

    <TextView
        android:id="@+id/ThirdText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </TextView>

    <TextView
        android:id="@+id/FourthText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </TextView>
</LinearLayout>
Harsha
  • 659
  • 10
  • 23
  • It doesn't work because i want to change height of each row to make the full listview suit screen dimensions, not just the layout_height of listview... – smartmouse Oct 06 '14 at 09:36
  • Can you update your code in the question if you have made any changes? – Harsha Oct 06 '14 at 09:41
  • I tested the above result and it worked for me. hope it'll work for you also. – Harsha Oct 06 '14 at 09:59
  • I'm trying to apply the first solution. I write this code the get the screen dimension: http://pastebin.com/SUR4EPZR but now i don't know how to use it. I inflate listview through XML with this: ListView lview = (ListView) rootView.findViewById(R.id.listview); – smartmouse Oct 06 '14 at 11:05
  • http://stackoverflow.com/questions/3821671/dynamically-changing-the-linearlayout-width-or-height-on-android check the above link. – Harsha Oct 06 '14 at 11:10