Julian Julian - 1 month ago 9
Android Question

Why isn't the whole screen used?

when i execute this code on my phone, it is only filled like half of the screen. Even when I add more views, all that changes is the fact that I can scroll now, but overall the screen isn't used more. So my guess is, that I made a mistake with the scroll view, but where? Glad to hear any suggestions on how I can make the Layout use the whole screen. Thank you in advance!

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_mahlzeiten"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="3">

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="produkt"
android:layout_weight="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="produkt"
android:layout_weight="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="produkt"
android:layout_weight="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="produkt"
android:layout_weight="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="produkt"
android:layout_weight="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="produkt"
android:layout_weight="1"/>



</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">

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

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-" />

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+" />

</LinearLayout>

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

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-" />

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+" />

</LinearLayout>

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

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-" />

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+" />

</LinearLayout>

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

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-" />

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+" />

</LinearLayout>

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

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-" />

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+" />

</LinearLayout>

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

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-" />

<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+" />

</LinearLayout>
</LinearLayout>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speisen hinzuf├╝gen"/>


</LinearLayout>





</LinearLayout>
</ScrollView>

Answer

You need to integrate all of your layouts into a general layout. Something like this would be usefull for you:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_mahlzeiten"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:text="produkt" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:text="produkt" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:text="produkt" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:text="produkt" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:text="produkt" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:text="produkt" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical">

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

                        <Button
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:text="-" />

                        <Button
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:text="+" />

                    </LinearLayout>

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

                        <Button
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:text="-" />

                        <Button
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:text="+" />

                    </LinearLayout>

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

                        <Button
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:text="-" />

                        <Button
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:text="+" />

                    </LinearLayout>

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

                        <Button
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:text="-" />

                        <Button
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:text="+" />

                    </LinearLayout>

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

                        <Button
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:text="-" />

                        <Button
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:text="+" />

                    </LinearLayout>

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

                        <Button
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:text="-" />

                        <Button
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:text="+" />

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>
        </ScrollView>


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Speisen hinzuf├╝gen" />

    </LinearLayout>

</LinearLayout>

As you could see, the button is at the bottom and the ScrollView will work when you add few items more.

Have a good day!