Okay I solved it myself. The problem was if an background image is added the page won't scroll until the image is bigger than the device size
ScrollView not working in Relative Layout
-
20-06-2023 - |
Question
I'm working on a Login page for my app . The ScrollView isn't working and I'm unable to find out why. I've tried changing the layout_height of ScrollView to wrap_content and some arbitrary value like 900dp and same with the relative layout inside but still no luck. When the soft keyboard appears and I fill in details in say 1st editText view and want to scroll down to fill the second one without closing the Soft Keyboard. The page isn't scrolling. Here is the Code
<?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"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:linksClickable="true"
android:text="New to scheduLAWyer ? Sign Up" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/register"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:text="Login" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:linksClickable="true"
android:text="Forgot Password" />
<ImageView
android:id="@+id/imageView123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/schedulawyer" />
<LinearLayout
android:id="@+id/lll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/login"
android:layout_alignRight="@+id/login"
android:layout_below="@+id/imageView123"
android:background="@drawable/rounded_corner"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Email/Phone"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
Solution
OTHER TIPS
Just wrap your RelativeLayout
within one LinearLayout
with android:layout_height="fill_parent"
. That will give the ScrollView
content to expand to.
The other, somewhat awkward approach could be to do it programmatically -- as described in this answer.
Change the height of the RelativeLayout
to wrap_content
.
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
// widgets afer this
Try this and see what happens...
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:linksClickable="true"
android:text="New to scheduLAWyer ? Sign Up" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/register"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:text="Login" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:linksClickable="true"
android:text="Forgot Password" />
<ImageView
android:id="@+id/imageView123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/schedulawyer" />
<LinearLayout
android:id="@+id/lll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/login"
android:layout_alignRight="@+id/login"
android:layout_below="@+id/imageView123"
android:background="@drawable/rounded_corner"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Email/Phone"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
</LinearLayout>
</ScrollView>
</RelativeLayout>