Linear Layout in Scroll View not working when tested on phone
I've put Linear Layout in Scroll View in a Relative Layout and when I test it on the phone it is not scrolling down
I've put the image view and heading Register outside scroll view and the text boxes and a submit button inside the linear layout which I placed inside the scroll view .
Please help with the code I've been trying for so long . I've tried Relative layout inside scroll view and the design shows correctly but when tested on phone it is not scrolling down in the phone (In both cases)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/MistyRose"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp">
<ImageView
android:id="@+id/imageView2"
android:layout_width="203dp"
android:layout_height="95dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:background="@drawable/logo" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignBottom="@id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="-120dp"
android:text="Register"
android:textColor="@color/Black"
android:textSize="40dp"
android:textStyle="italic" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/textView"
android:layout_marginBottom="-520dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/Namebox"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:background="@drawable/edittext"
android:ems="25"
android:hint="Name"
android:inputType="text" />
<EditText
android:id="@+id/Enailbox"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:background="@drawable/edittext"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/Phonebox"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:background="@drawable/edittext"
android:hint="Phone"
android:inputType="phone" />
<EditText
android:id="@+id/Passbox1"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:background="@drawable/edittext"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/Passbox2"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:background="@drawable/edittext"
android:hint="Password Confirm"
android:inputType="textPassword" />
<Button
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginStart="100dp"
android:layout_marginTop="50dp"
android:background="@drawable/button"
android:text="Submit" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Consider using constrain layout don't set negative margin to your widgets , for sroll view use "fillViewPort=true", use this snippen as a base layout and modify it according to your needs.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ItemDetailActivity"
tools:ignore="MergeRootFrame">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@+id/toolbar">
<android.support.v7.widget.Toolbar
android:id="@+id/detail_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/item_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.LinearLayoutCompat
android:orientation="vertical"
android:padding="16dp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_marginTop="16dp"
android:id="@+id/Namebox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="25"
android:hint="Name"
android:inputType="text"/>
<EditText
android:layout_marginTop="16dp"
android:id="@+id/Enailbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"/>
<EditText
android:layout_marginTop="16dp"
android:id="@+id/Phonebox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone"
android:inputType="phone" />
<EditText
android:layout_marginTop="16dp"
android:id="@+id/Passbox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:layout_marginTop="16dp"
android:id="@+id/Passbox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password Confirm"
android:inputType="textPassword" />
<Button
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:layout_margin="@dimen/fab_margin"
app:layout_anchor="@+id/item_detail_container"
app:layout_anchorGravity="top|end"
app:srcCompat="@drawable/ic_done_black_24dp" />
</android.support.design.widget.CoordinatorLayout>
I've modified the layout and removed negative margins. Please have a look following XML and try this. The scroll will work as per your expectation.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/MistyRose"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="203dp"
android:layout_height="95dp"
android:background="@drawable/logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_below="@id/imageView2"
android:layout_centerHorizontal="true"
android:text="Register"
android:layout_marginTop="100dp"
android:textColor="@color/Black"
android:textSize="40dp"
android:textStyle="italic" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textView"
android:layout_marginTop="50dp"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/Namebox"
android:layout_width="300dp"
android:layout_height="50dp"
android:background="@drawable/edittext"
android:layout_marginStart="30dp"
android:ems="25"
android:hint="Name"
android:inputType="text" />
<EditText
android:id="@+id/Enailbox"
android:layout_width="300dp"
android:layout_height="50dp"
android:background="@drawable/edittext"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/Phonebox"
android:layout_width="300dp"
android:background="@drawable/edittext"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:hint="Phone"
android:inputType="phone" />
<EditText
android:id="@+id/Passbox1"
android:layout_width="300dp"
android:background="@drawable/edittext"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/Passbox2"
android:layout_width="300dp"
android:background="@drawable/edittext"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:hint="Password Confirm"
android:inputType="textPassword" />
<Button
android:layout_width="150dp"
android:layout_height="50dp"
android:background="@drawable/button"
android:layout_marginStart="100dp"
android:layout_marginTop="50dp"
android:text="Submit" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Please remove negative margin android:layout_marginBottom="-520dp"
from ScrollView and just try below code:
<?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="@color/MistyRose"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="203dp"
android:layout_height="95dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:background="@drawable/logo" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_below="@id/imageView2"
android:layout_centerHorizontal="true"
android:text="Register"
android:textColor="@color/Black"
android:textSize="40dp"
android:textStyle="italic" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textView"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/Namebox"
android:layout_width="300dp"
android:layout_height="50dp"
android:background="@drawable/edittext"
android:layout_marginStart="30dp"
android:ems="25"
android:hint="Name"
android:inputType="text"
android:layout_marginLeft="30dp" />
<EditText
android:id="@+id/Enailbox"
android:layout_width="300dp"
android:layout_height="50dp"
android:background="@drawable/edittext"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:hint="Email"
android:inputType="textEmailAddress"
android:layout_marginLeft="30dp" />
<EditText
android:id="@+id/Phonebox"
android:layout_width="300dp"
android:background="@drawable/edittext"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:hint="Phone"
android:inputType="phone"
android:layout_marginLeft="30dp" />
<EditText
android:id="@+id/Passbox1"
android:layout_width="300dp"
android:background="@drawable/edittext"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:hint="Password"
android:inputType="textPassword"
android:layout_marginLeft="30dp" />
<EditText
android:id="@+id/Passbox2"
android:layout_width="300dp"
android:background="@drawable/edittext"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:hint="Password Confirm"
android:inputType="textPassword"
android:layout_marginLeft="30dp" />
<Button
android:layout_width="150dp"
android:layout_height="50dp"
android:background="@drawable/button"
android:layout_marginStart="100dp"
android:layout_marginTop="50dp"
android:text="Submit"
android:layout_marginLeft="100dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
In your xml code you give the nagative margin ,just remove them your layout will scroll according to your need.
Try this @Thomas,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<LinearLayout
android:id="@+id/headerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="203dp"
android:layout_height="95dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:background="@drawable/logo" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignBottom="@id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="-120dp"
android:text="Register"
android:textColor="@color/Black"
android:textSize="40dp"
android:textStyle="italic" />
</LinearLayout>
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/headerView"
android:layout_marginBottom="@dimen/dimen_50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/Namebox"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:background="@drawable/edittext"
android:ems="25"
android:hint="Name"
android:inputType="text" />
<EditText
android:id="@+id/Enailbox"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:background="@drawable/edittext"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/Phonebox"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:background="@drawable/edittext"
android:hint="Phone"
android:inputType="phone" />
<EditText
android:id="@+id/Passbox1"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:background="@drawable/edittext"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/Passbox2"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:background="@drawable/edittext"
android:hint="Password Confirm"
android:inputType="textPassword" />
<Button
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginStart="100dp"
android:layout_marginTop="50dp"
android:background="@drawable/button"
android:text="Submit" />
</LinearLayout>
</ScrollView>
更改滚动视图属性
android:layout_height="match_parent"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.