Search code examples
androidandroid-layoutscrollviewlandscape-portrait

ListView not displaying all items in Landscape mode


I have an Activity that fits the screen size in portrait mode like shown in image below

enter image description here

Problem is when i rotate screen, the ListView collapses and displays only one item like shown in image below

enter image description here

What is causing this issue and how can i fix it ?

Here's my xml layout file

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_dark"
        android:orientation="vertical">

        <TextView
            android:id="@+id/randomWordLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="dfdfdf"
            android:textColor="@android:color/white"
            android:textSize="50sp"
            android:padding="10dp"
            android:layout_gravity="center_horizontal"/>

        <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context="com.example.yousaf.mcqstest.GamePlay"
            android:id="@+id/listViewContainer">

            <ListView
                android:id="@+id/myListView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:background="#fff"
                android:dividerHeight="7dp"
                android:padding="10dp">
            </ListView>

        </android.support.constraint.ConstraintLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:paddingTop="15dp">

            <TextView
                android:id="@+id/textPoints"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:text="Points : "
                android:textSize="22sp"
                android:textColor="#fff"
                android:layout_gravity="center"
                android:gravity="center"/>

            <TextView
                android:id="@+id/textPointsValue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textSize="22sp"
                android:textColor="#fff"
                android:layout_gravity="center" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:paddingTop="15dp">

            <TextView
                android:id="@+id/textCorrectAnswers"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:text="Correct Answers : "
                android:textSize="22sp"
                android:textColor="#fff" />

            <TextView
                android:id="@+id/textCorrectAnswersValue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textSize="22sp"
                android:textColor="#fff" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:paddingTop="20dp">

            <TextView
                android:id="@+id/textWrongAnswers"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:text="Wrong Answers   : "
                android:textSize="22sp"
                android:textColor="#fff" />

            <TextView
                android:id="@+id/textWrongAnswersValue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textSize="22sp"
                android:textColor="#fff" />

        </LinearLayout>

        <Button
            android:id="@+id/btnStopGame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:padding="15dp"
            android:background="#222"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="10dp"/>

    </LinearLayout>

</ScrollView>

Solution

  • So i solved the issue mentioned in my question by having

    1- Separate layout file for landscape mode. Made a a new folder named layout-land inside res folder and then copied the layout file included in my question in to layout-land folder and then made appropriate changes according to landscape portrait.

    2- Having fixed height for ListView. In my case i set the height of ListView to 190dp.

    Now my layout in landscape looks like shown in image below

    enter image description here