2

I have a RelativeLayout in my app, but when the phone is in landscape mode it won't scroll. I read that I need to put it inside a ScrollView but I keep getting errors, so I might not be doing it right.

Any tips?

Thanks in advance.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff8400"
android:orientation="vertical" >

<EditText
    android:id="@+id/txt_nomeSpot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:ems="10"
    android:hint="@string/nome"
    android:inputType="text" 
    android:gravity="center" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/txt_coordSpot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txt_nomeSpot"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:hint="@string/coordenadas"
    android:inputType="text" 
    android:ems="10"
    android:gravity="center" />

<EditText
    android:id="@+id/txt_classSpot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txt_coordSpot"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:hint="@string/classif"
    android:inputType="text" 
    android:ems="10"
    android:gravity="center" />

<EditText
    android:id="@+id/txt_tipoSpot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txt_classSpot"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:hint="@string/tipo"
    android:inputType="text" 
    android:ems="10" 
    android:gravity="center"/>

<EditText
    android:id="@+id/txt_subtipoSpot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txt_tipoSpot"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:hint="@string/subtipo"
    android:inputType="text" 
    android:ems="10"
    android:gravity="center" />

<EditText
    android:id="@+id/txt_moradaSpot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txt_subtipoSpot"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:hint="@string/morada"
    android:inputType="text" 
    android:ems="10"
    android:gravity="center" />

<Button
    android:id="@+id/btn_submitSpot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txt_moradaSpot"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="@string/submeter" />

2 Answers 2

5

Simply have the ScrollView as the top level element. Your existing RelativeLayout then goes inside.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff8400"
        android:orientation="vertical" >

        ...other existing code here

    </RelativeLayout>

</ScrollView>

EDIT: I've changed the height of your RelativeLayout to "wrap_content" since kabuko is correct in what he says in his answer.

4
  • I did just as you told me and it came out like this: s30.postimg.org/g3hw5hfjl/unnamed.png What did I did wrong?
    – Faquir
    Jan 11, 2014 at 1:06
  • My mistake - try setting the RelativeLayout width back to "match_parent" but leave the height as "wrap_content".
    – NigelK
    Jan 11, 2014 at 1:11
  • Now it's like this: s23.postimg.org/tnfbo9rvv/Screenshot_2014_01_11_01_17_02.png It's getting closer thow...
    – Faquir
    Jan 11, 2014 at 1:17
  • Try moving the android:background statement to the scrollview. Does it then look like your original version? If not, post that image too and perhaps ask kabuko for further advice under his answer.
    – NigelK
    Jan 11, 2014 at 1:30
2

You do need a ScrollView, but it doesn't make sense for the View inside to be of height match_parent. That would make the inside View the same size as the ScrollView leaving nothing to scroll. You need to set the inner View (in this case your RelativeLayout) tall enough to show all of its content. Generally, that means wrap_content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.