Search code examples
androidlayoutscrollviewshift

Android How to add ScrollView? ScrollView shits layout


I have a problem with adding ScrollView into my layout. So, when I add ScrollView (match_parent) into my layout, it shifts my layout into right side. Hereby, I see only background picture on the screen of the device. All Buttons, Textviews, and Edittexts shifted into right. By the way, I wrote code for only portrait layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context="com.example.rauf.myapplication.Main2Activity"
android:background="@drawable/bii3">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:layout_editor_absoluteY="8dp"
    tools:layout_editor_absoluteX="8dp"
    android:paddingLeft="60dp"
    android:paddingRight="58dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingTop="42dp">

        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:src="@drawable/kamera" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_weight="0.5"
        android:paddingTop="35dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="16dp">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:src="@drawable/message" />

            <EditText
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:background="#00000000"
                android:hint="email address"
                android:layout_marginLeft="15dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#4a5a71"></LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="16dp">
            <ImageView
                android:id="@+id/log"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:src="@drawable/usrusr"/>
            <EditText
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:background="#00000000"
                android:hint="username"
                android:layout_marginLeft="15dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#4a5a71"></LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="16dp">
            <ImageView
                android:id="@+id/red"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:src="@drawable/pswrd"/>
            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:background="#00000000"
                android:hint="password"
                android:layout_marginLeft="15dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#4a5a71"></LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="16dp">
            <ImageView
                android:id="@+id/red2"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:src="@drawable/pswrd"/>
            <EditText
                android:id="@+id/passwordconf"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:background="#00000000"
                android:hint="password confirm"
                android:layout_marginLeft="15dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#4a5a71"></LinearLayout>
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_background2"
            android:text="SIGN UP"
            android:onClick="clickButton2"
            android:clickable="true"
            android:layout_marginTop="20dp"
            android:textColor="#ffffff"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginTop="30dp"
            android:onClick="clickFunction2"
            android:text="Already have an Account ?"
            android:textColor="#000000" />
    </LinearLayout>
</LinearLayout>
</LinearLayout>

I want ScrollView for this:

desired

But I end up with this:

enter image description here


Solution

  • This needs to be the settings for the top three layouts You had the last one set to 600dp which was why it was pushing everything further down , change it to wrap_content:)

      <ScrollView 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">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                tools:layout_editor_absoluteY="8dp"
                tools:layout_editor_absoluteX="8dp"
                android:paddingLeft="60dp"
                android:paddingRight="58dp">
    
                <LinearLayout
                    android:id="@+id/CHANGE_THIS_ONE"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" <<<<<<<<<
                    android:layout_weight="2"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:paddingTop="42dp">