Search code examples
javaandroidandroid-scrollview

ScrollView going behind button in Android


Why is the scrollview going behind the button here? I want the scrollview to stretch in height depending on the device but cant get it to work as it always goes behind the button.

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/detail_view"
    style="@style/DetailView">

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

        <TextView
            style="@style/TextView"
            android:id="@+id/textview_message_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
        </TextView>

         <TextView
            style="@style/TextView"
            android:id="@+id/textview_message_date"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
        </TextView>

        <ScrollView 
            android:id="@+id/scrollview_message_text" 
            android:layout_height="wrap_content" 
            android:paddingBottom="20dip"
            android:layout_width="fill_parent"> 
             <TextView
                style="@style/TextView"
                android:id="@+id/textview_message_text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </TextView>
        </ScrollView>

    </LinearLayout>

     <LinearLayout style="@style/DetailView.Buttons"
          android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:orientation="vertical">

         <Button
            style="@style/Button"
            android:layout_marginTop="5dip"
            android:id="@+id/button_view_file"
            android:text="@string/viewfile"/>

    </LinearLayout>

</RelativeLayout>

Solution

  • Here is a working copy of what I think you are looking for

    1: Moved the buttons to the top of the relative layout ( added an id ) android:layout_alignParentBottom="true"

     <LinearLayout android:id="@+id/buttons"
                  android:layout_alignParentBottom="true"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
    
           <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginTop="5dip"
                android:id="@+id/button_view_file"
                android:text="Button"/>
    
       </LinearLayout>
    

    2: Add in the layout containing the scroll change the width and height to match parent but ( CRITICAL ) add android:layout_above="@id/buttons"

      <LinearLayout
            android:layout_above="@id/buttons"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray"
            android:gravity="center"
            android:orientation="vertical" >
    

    So putting it all together:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:id="@+id/detail_view"
                >
    
    
      <!-- Buttons at the top of layout but aligned to the bottom -->
      <LinearLayout android:id="@+id/buttons"
                  android:layout_alignParentBottom="true"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
    
        <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginTop="5dip"
                android:id="@+id/button_view_file"
                android:text="Button"/>
    
     </LinearLayout>
    
    
     <!-- Linear layout to fill screen, but assigned to layout above the buttons -->
     <LinearLayout
            android:layout_above="@id/buttons"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray"
            android:gravity="center"
            android:orientation="vertical" >
    
         <TextView
                 android:id="@+id/textview_message_title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center">
         </TextView>
    
         <TextView
                android:id="@+id/textview_message_date"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center">
         </TextView>
    
         <ScrollView
                android:id="@+id/scrollview_message_text"
                android:layout_height="wrap_content"
                android:paddingBottom="20dip"
                android:layout_width="fill_parent">
            <TextView
                    android:id="@+id/textview_message_text"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
            </TextView>
        </ScrollView>
    
     </LinearLayout>
    
    
    
    </RelativeLayout>
    

    I removed the styles so I could see it in the layout an make sure this worked before posting. Hope this helps.

    Relative layout allows you to layer elements on top of each other. So if that is not what you are looking for you need to make use of the toLeft, toRight, above, below directives to nudge the layouts into the correct position.