Devrath
Devrath

Reputation: 42824

How to place elements one below another in constraint layout whose parent is scroll view

What I am having:

I am having 4 widgets, placed inside a constraint layout, whose parent is a scroll view


What I am trying to do:

I am trying to display widgets one below another


What is happening:

Widgets are placed one above another


MyLayout

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fillViewport="true">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <EditText
            android:id="@+id/edtNameId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/str_name"
            android:inputType="text"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <EditText
            android:id="@+id/edtPwdId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/str_password"
            app:layout_constraintBottom_toBottomOf="@+id/edtNameId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <TextView
            android:id="@+id/txtChangePwdId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/str_change_password"
            app:layout_constraintBottom_toBottomOf="@+id/edtPwdId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <Button
            android:id="@+id/btnSubmitId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/str_submit"
            app:layout_constraintBottom_toBottomOf="@+id/txtChangePwdId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>


    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

Output:

enter image description here

Upvotes: 2

Views: 2519

Answers (1)

C&#244;ng Hải
C&#244;ng Hải

Reputation: 5241

You should use layout_constraintTop_toBottomOf instead of app:layout_constraintBottom_toBottomOf try this

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fillViewport="true">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <EditText
            android:id="@+id/edtNameId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/str_name"
            android:inputType="text"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <EditText
            android:id="@+id/edtPwdId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/str_password"
            app:layout_constraintTop_toBottomOf="@+id/edtNameId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <TextView
            android:id="@+id/txtChangePwdId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/str_change_password"
            app:layout_constraintTop_toBottomOf="@+id/edtPwdId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <Button
            android:id="@+id/btnSubmitId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/str_submit"
            app:layout_constraintTop_toBottomOf="@+id/txtChangePwdId"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>


    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

Upvotes: 5

Related Questions