iadcialim24
iadcialim24

Reputation: 4035

Android Constraint Layout: How to vertically align view with another view

I want the Edit textview to be vertically aligned to PROFILE. I tried a lot of things that makes sense.

Using these constraints

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/parentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">
        <TextView
            android:id="@+id/titleTxt"
            android:layout_width="wrap_content"
            android:text="PROFILE"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:textStyle="bold"
            android:layout_marginTop="24dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
        />

        <TextView
            android:id="@+id/editBtn"
            android:text="Edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="18dp"
            android:textSize="20sp"
            android:textColor="@android:color/white"
            android:padding="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="titleTxt"
            app:layout_constraintBottom_toBottomOf="titleTxt"
        />
    </androidx.constraintlayout.widget.ConstraintLayout>

This is my current output

enter image description here

Upvotes: 0

Views: 53

Answers (4)

Sajith
Sajith

Reputation: 771

try this,

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
    android:id="@+id/titleTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="72dp"
    android:text="PROFILE"
    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@+id/editBtn"
    app:layout_constraintEnd_toStartOf="@+id/editBtn" />

<TextView
    android:id="@+id/editBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_marginEnd="30dp"
    android:text="Edit"
    android:textColor="@android:color/black"
    android:textSize="20sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Upvotes: 0

Ankit Tale
Ankit Tale

Reputation: 2004

You need to set a baseline from edit Textview to profile Textview

app:layout_constraintBaseline_toBaselineOf="@+id/titleTxt"

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">
    <TextView
        android:id="@+id/titleTxt"
        android:layout_width="wrap_content"
        android:text="PROFILE"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        android:textStyle="bold"
        android:layout_marginTop="24dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />

    <TextView
        android:id="@+id/editBtn"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_marginEnd="16dp"
        android:padding="8dp"
        android:text="Edit"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        app:layout_constraintBaseline_toBaselineOf="@+id/titleTxt"
        app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Mehul Kabaria
Mehul Kabaria

Reputation: 6632

You have to align your Edit TextView like below.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/parentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">
    <TextView
            android:id="@+id/titleTxt"
            android:layout_width="wrap_content"
            android:text="PROFILE"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:textStyle="bold"
            android:layout_marginTop="24dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
    />

    <TextView
            android:id="@+id/editBtn"
            android:text="Edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:textSize="20sp"
            android:textColor="@android:color/white"
            android:padding="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/titleTxt"
            app:layout_constraintBottom_toBottomOf="@+id/titleTxt"
    />
</androidx.constraintlayout.widget.ConstraintLayout>

output

enter image description here

Upvotes: 1

Md. Nowshad Hasan
Md. Nowshad Hasan

Reputation: 626

You can try -

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/colorPrimary"
    tools:context=".activity.WebviewActivity">

    <TextView
        android:id="@+id/titleTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="PROFILE"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/editBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="18dp"
        android:layout_marginEnd="16dp"
        android:padding="8dp"
        android:text="Edit"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Upvotes: 0

Related Questions