Dishonered
Dishonered

Reputation: 8851

How to modify chain length in constraint layout?

Here is my code

<?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/nav_and_action_bar_color"
 >

<ImageView
    android:id="@+id/some_person"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/some_person"
    app:layout_constraintBottom_toTopOf="@+id/et_username"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<EditText
    android:id="@+id/et_username"
    android:layout_width="@dimen/login_page_text_fields_width"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@android:color/white"
    android:hint="@string/email_hint"
    android:padding="10dp"
    app:layout_constraintBottom_toTopOf="@+id/et_password"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/some_person" />

<EditText
    android:id="@+id/et_password"
    android:layout_width="@dimen/login_page_text_fields_width"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@android:color/white"
    android:hint="@string/password_hint"
    android:inputType="textPassword"
    android:padding="10dp"
    app:layout_constraintBottom_toTopOf="@+id/login_button"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/et_username" />

<Button
    android:id="@+id/login_button"
    android:layout_width="@dimen/login_button_width"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@color/colorAccent"
    android:elevation="5dp"
    android:onClick="clickedOnLogin"
    android:text="@string/login_button"
    android:textColor="@android:color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/et_password" />
</android.support.constraint.ConstraintLayout>

and i seem to be getting this as a result

emulator screenshot

How do i bring the elements together? I want them to be close together.

Upvotes: 3

Views: 1714

Answers (3)

Bhupat Bheda
Bhupat Bheda

Reputation: 1978

Use below code to get the desired layout. Changes in the below code of yours is "app:layout_constraintVertical_chainStyle="packed".

<?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/nav_and_action_bar_color">

    <ImageView
        android:id="@+id/some_person"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/some_person"
        app:layout_constraintBottom_toTopOf="@+id/et_username"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />


    <EditText
        android:id="@+id/et_username"
        android:layout_width="@dimen/login_page_text_fields_width"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@android:color/white"
        android:hint="@string/email_hint"
        android:padding="10dp"
        app:layout_constraintBottom_toTopOf="@+id/et_password"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/some_person" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="@dimen/login_page_text_fields_width"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@android:color/white"
        android:hint="@string/password_hint"
        android:inputType="textPassword"
        android:padding="10dp"
        app:layout_constraintBottom_toTopOf="@+id/login_button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/et_username" />

    <Button
        android:id="@+id/login_button"
        android:layout_width="@dimen/login_button_width"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@color/colorAccent"
        android:elevation="5dp"
        android:onClick="clickedOnLogin"
        android:text="@string/login_button"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/et_password" />
</android.support.constraint.ConstraintLayout>

Upvotes: 3

Dishonered
Dishonered

Reputation: 8851

I managed to bring the elements close together. According to this page

Chains are controlled by attributes set on the first element of the chain (the "head" of the chain):

So i added this to <ImageView> tag

app:layout_constraintVertical_chainStyle="packed"

and since i did not want it to be at the center vertically i also added this to the same tag

app:layout_constraintVertical_bias="0.4"

So that it is not at the center but is slightly biased towards to the top.

Upvotes: 1

jantursky
jantursky

Reputation: 1130

You need to define variable for every view, where you want to apply chaining logic. Pick just one:

app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintVertical_chainStyle="spread_inside"

Upvotes: 6

Related Questions