Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

my listview doesn't fill parent

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"

    <TextView 
        android:id="@+id/tv_test"
        android:text="Test "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    <ListView 
        android:id="@+id/list_test"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tv_test"

  ></ListView>
</RelativeLayout>

And everything is in a ScrollView, i don't know why but the relative layout seems to set a wrap content. And it makes the ListView small and not taking the whole place.

I don't know if it's useful but here's my java code :

public class ShowView extends Activity{
ListView lv;
ArrayAdapter<String> adapter;
String[] myList = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Height", "Nine", "Ten"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show);
    lv = (ListView) findViewById(R.id.list_test);
    adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, myList);
    adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
    lv.setAdapter(adapter);

}
}
like image 620
Tsunaze Avatar asked Aug 24 '11 12:08

Tsunaze


2 Answers

I think you ScrollView is not filling the entire screen. Use this inside scrollView

android:fillViewport="true"
like image 107
droid kid Avatar answered Nov 16 '22 13:11

droid kid


Replace the RelativeLayout with a vertical LinearLayout, and set android:layout_weight="1" and android:layout_height="wrap_content" on your ListView:

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <TextView 
        android:id="@+id/tv_test"
        android:text="Test "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ListView 
        android:id="@+id/list_test"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_below="@id/tv_test" />
</LinearLayout>

Check out this article for further information on layout weights.

Also, I would remove the ScrollView altogether. ListView manages scrolling itself, and you will run in to all kinds of problems if you embed it inside a container which handles scrolling. If you want to scroll @is/tv_test along with your ListView content, then consider putting it inside your ListView as a header. If you want it to remain static when the ListView scrolls, then keep it outside the ListView.

like image 39
Mark Allison Avatar answered Nov 16 '22 14:11

Mark Allison