1

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> 
2
  • Don't leave Bottom Padding, I reckon. Btw, can't you put button in the ScrollView? Commented Jan 7, 2015 at 14:41
  • from the posted code I don't see why the View File button is where it is. probably something in the style? Commented Jan 7, 2015 at 14:56

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow that worked. I assume this will work on any display. Thanks again!
Glad to help, it should work fine. RelativeLayout is a lot more flexible then nesting LinearLayouts. It just takes a little while to get comfortable with it.
1

This happens because you use Relative layout as the root layout. So both layout are placed on the same place but the button layout is declared after the scrollview so it is shown above the scrollview The opposite would happen if you were defining the button before the scrollview

So you should define it as shown below to achieve what you want.

<?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:layout_above="@id/layout_bottom" 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 android:id="@+id/layout_bottom" 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> 

or you can try and use LinearLayout as the root view and set

layout_height = "0dp" layout_weight = "1" 

to the layout containing the scrollview

using both these way will make the button stick to the bottom of the layout and the top layout will get all the remaining height.

Good Luck

Comments

0

Why is the scrollview going behind the button here?

Because it is coming first in your layout, ReletiveLayout and FrameLayout can be used to provide Z-ordering, So every child which comes later in your layout having higher z value so it goes up.Add android:layout_below = "@id/scrollview_message_text" to your last linearlayout or order your views within in mind of this characteristic of reletivelayout.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.