ntm
ntm

Reputation: 741

android layout dynamic width of listviews

in my layout i have 5 listviews next to each other, and a want them all together to match the screen size so that everyone of them has a width of 1/5 of the screens width. match parent doesnt work because then every listview is as big as the screen.

actually i use this:

<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scrollView"
            android:layout_marginTop="25dp">

        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                >

            <ListView
                    android:layout_width="95dp"
                    android:layout_height="700dp"
                    android:id="@+id/listView2"
                    android:layout_gravity="center_horizontal|top"/>

            <ListView
                    android:layout_width="95dp"
                    android:layout_height="700dp"
                    android:id="@+id/listView3"
                    android:layout_gravity="center_horizontal|top"/>

            <ListView
                    android:layout_width="95dp"
                    android:layout_height="700dp"
                    android:id="@+id/listView4"
                    android:layout_gravity="center_horizontal|top"/>

            <ListView
                    android:layout_width="95dp"
                    android:layout_height="700dp"
                    android:id="@+id/listView5"
                    android:layout_gravity="center_horizontal|top"/>

            <ListView
                    android:layout_width="95dp"
                    android:layout_height="700dp"
                    android:id="@+id/listView6"
                    android:layout_gravity="center_horizontal|top"/>

        </LinearLayout>
    </ScrollView>

but that of course doesnt work for different screen sizes.

Upvotes: 0

Views: 1030

Answers (2)

Gokhan Arik
Gokhan Arik

Reputation: 2766

Put android:weightSum="5" to your LinearLayout and add android:layout_weight="1" to your each ListView. You might need to set your width to 0.

I also wouldn't recommend putting ListView inside ScrollView.

Upvotes: 0

M-Wajeeh
M-Wajeeh

Reputation: 17284

Don't EVER put ListView inside ScrollView, anyways here is how you do it:

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

        <ListView
                android:layout_width="0dp"
                android:layout_height="fill_parent" android:layout_weight="20"
                android:id="@+id/listView2" />

        <ListView
                android:layout_width="0dp"
                android:layout_height="fill_parent" android:layout_weight="20"
                android:id="@+id/listView3" />

        <ListView
                android:layout_width="0dp"
                android:layout_height="fill_parent" android:layout_weight="20"
                android:id="@+id/listView4" />

        <ListView
                android:layout_width="0dp"
                android:layout_height="fill_parent" android:layout_weight="20"
                android:id="@+id/listView5" />

        <ListView
                android:layout_width="0dp"
                android:layout_height="fill_parent" android:layout_weight="20"
                android:id="@+id/listView6" />

    </LinearLayout>

Upvotes: 1

Related Questions