Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinearLayout in ScrollView: Make last child have minHeight and fill empty space if necessary

currently I have a LinearLayout with a bunch of other Views as children. Almost all of them use the height of wrap_content and the main LinearLayout uses wrap_content as well. However, the last child in the main LinearLayout is a RelativeLayout which uses match_parent. It contains a MapView and a few buttons.

enter image description here

I want to add some more children before the last child. By doing this, the space the map can take up gets smaller and smaller the more children I add. This can go so far, that the map is not visible anymore at all because other children, even with wrap_content, are taking up the whole space.

My idea or whish is to put the main LinearLayout inside a ScrollView. Then I could have unlimited children.

My question is: How can I make my RelativeLayout take up the rest of the screen or be at least 200dp in height so that I can be sure that it is visible even if there is not enough space on screen to display it at first?

enter image description here

My layout might/shall look like this:

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

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

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

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

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

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

            <com.google.android.gms.maps.MapView
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button ... />
            <Button ... />
            <Button ... />
        </RelativeLayout>
    </LinearLayout>
</ScrollView>
like image 810
xxtesaxx Avatar asked Dec 20 '25 15:12

xxtesaxx


2 Answers

Use this, you have to use dp and weight both, you should use weight because when you have less child you want your RealtiveLayout to cover the empty space, and dp when your child a much then also you want to maintain a size for your RelativeLayout

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

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

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

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

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

             <!--Use dp also, because when you have more childs, 
               it wants a dp to maitain a size-->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_weight="1">

                <com.google.android.gms.maps.MapView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <Button ... />
                <Button ... />
                <Button ... />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>
like image 50
Ranjan Avatar answered Dec 23 '25 03:12

Ranjan


Try below code it will help you.

<ScrollView
       android:layout_width="match_parent"
      **android:fillViewport="true"**
        android:layout_height="match_parent" >

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

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

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

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

            <RelativeLayout
                android:layout_width="match_parent"
               **android:layout_height="0dp"
                android:layout_weight="1"**>

                <com.google.android.gms.maps.MapView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <Button ... />
                <Button ... />
                <Button ... />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>
like image 31
Mavya Soni Avatar answered Dec 23 '25 05:12

Mavya Soni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!