S.J. Lim
S.J. Lim

Reputation: 3165

How to declare layout to automatically adjust height of ListView in multiple devices?

I'm trying to layout buttons in buttom of device.

Following pic1 is layout on 4.0 inch device, pic2 is on 4.7 inch device

and there is tried xml code.

buttons arranged correct position on 4.0 inch, but not on 4.7inch.

I'd like to automatically adjust height of List View.

How to do that?


[pic1]

enter image description here


[pic2]

enter image description here


Here is xml code.

<?xml version="1.0" encoding="utf-8"?>

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

    <TextView
            android:id="@+id/txtLabel2"
            android:text="Buyed items"
            android:textSize="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <ListView
            android:id="@+id/lsv_buyedList"
            android:layout_width="match_parent"
            android:layout_height="450dp">
    </ListView>


    <LinearLayout
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <LinearLayout
                android:layout_weight="1"
                android:layout_marginLeft="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            <Button
                    android:id="@+id/btn_sortById"
                    android:text="SortById"
                    android:textSize="15dp"
                    android:layout_width="110dp"
                    android:layout_height="wrap_content"/>

            <Button
                    android:id="@+id/btn_sortByGrade"
                    android:text="SortByGrade"
                    android:textSize="15dp"
                    android:layout_width="110dp"
                    android:layout_height="wrap_content"/>
        </LinearLayout>

        <Button
                android:id="@+id/btn_confirm"
                android:text="OK"
                android:textSize="15dp"
                android:layout_width="100dp"
                android:layout_marginRight="10dp"
                android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>

Upvotes: 0

Views: 617

Answers (4)

Shakeeb Ayaz
Shakeeb Ayaz

Reputation: 6096

Replace android:layout_height="450dp" with android:layout_height="wrap_conent" from listView

Upvotes: 1

Kailash Dabhi
Kailash Dabhi

Reputation: 3513

copy this code and paste it.. It works i tried on my machine... enjoy..!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/txtLabel2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Buyed items"
    android:textSize="20dp" />

<ListView
    android:id="@+id/lsv_buyedList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

</ListView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/btn_sortById"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:text="SortById"
            android:textSize="15dp" />

        <Button
            android:id="@+id/btn_sortByGrade"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:text="SortByGrade"
            android:textSize="15dp" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_confirm"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:text="OK"
        android:textSize="15dp" />
</LinearLayout>

</LinearLayout>

Upvotes: 0

Ayman Mahgoub
Ayman Mahgoub

Reputation: 4220

Use the "weight" field for the ListView

android:layout_height="0dip"
android:layout_weight="1"

Upvotes: 2

Padma Kumar
Padma Kumar

Reputation: 20041

//Remove the hardcoded value from your listview android:layout_height="450dp"

and use weight for you listview

            <ListView
                android:id="@+id/lsv_buyedList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" >
        </ListView>

Upvotes: 0

Related Questions