Pattabi Raman
Pattabi Raman

Reputation: 5854

How to set height for the listview that created dynamically through java code?

I 'm trying to set the height of the listview which has been created through java code dynamically. I'm using 3 listviews(one below another) in a scrollview(to avoid hiding of lists). If i use scroll view, i cannot see all the list items of the listviews.Is there any idea to set height for those 3 list views through java coding in android? Any help is really appreciated and thanks in advance...

Upvotes: 0

Views: 4394

Answers (1)

thaussma
thaussma

Reputation: 9886

Don't put ListViews in ScrollViews! In Detail Information from the developers of Listview is available in the talk World of ListViews

It is possible to make the three ListViews share the space on the Screen by using the layoutWeight parameter.

Make a layout like this (you can also use code for that):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/listView1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"></ListView>
<ListView android:id="@+id/listView2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"></ListView>
<ListView android:id="@+id/listView3" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"></ListView>
</LinearLayout>

Here each ListView takes a third of the available space (all weights set to 1).

To set the layoutWeight in code use this line as example:

listview1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));

Upvotes: 3

Related Questions