2

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>
xxtesaxx
  • 6,175
  • 2
  • 31
  • 50

2 Answers2

3

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>
Ranjan
  • 1,326
  • 18
  • 38
  • Thank you, that is what I was looking for. I achieved something similar by setting minHeight="220dp" on my MapView, make the RelativeLayout layout_height = match_parent and the put the RelativeLayout in another LinearLayout which uses wrap_content. Your solution is cleane but also gives me a warning that I shall use height = 0dp (which will obviously not work). The only question left is: do you have a good resource explaining why weight = 1 will override the 200dp height if there is space available and ignore it if there is no space? And why do I use layout_height instead of minHeight? – xxtesaxx Dec 21 '16 at 06:46
  • I also have a very similar question where I not use a LinearLayout but a RecyclerView. Maybe you know a solution for this problem as well? http://stackoverflow.com/questions/41253811/make-last-item-viewholder-in-recyclerview-fill-rest-of-the-screen-or-have-a-mi – xxtesaxx Dec 21 '16 at 06:48
0

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>
Mavya Soni
  • 932
  • 7
  • 15