Ali
Ali

Reputation: 9994

setup constraint in a ConstraintLayout properly while using Guideline

I have following ConstraintLayout :

<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:id="@+id/detail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/contact_selector"
    android:minHeight="64dp">

    <FrameLayout
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@+id/guideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline">

        <TextView
            android:id="@+id/image_text"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:background="@drawable/contact_circle"
            android:gravity="center"
            tools:text="A.R" />

    </FrameLayout>

    <TextView
        android:id="@+id/contact_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:maxWidth="248dp"
        android:paddingLeft="16dp"
        android:paddingStart="16dp"
        android:textSize="17sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintStart_toEndOf="@+id/image"
        tools:text="Ali Rezaei" />

    <TextView
        android:id="@+id/phone_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp"
        android:paddingStart="16dp"
        android:textSize="13sp"
        app:layout_constraintStart_toStartOf="@+id/contact_name"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        tools:text="+989121895634" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <TextView
        android:id="@+id/phone_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-light"
        android:paddingEnd="8dp"
        android:paddingRight="8dp"
        android:textSize="13sp"
        app:layout_constraintBottom_toBottomOf="@+id/guideline"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        tools:text="Mobile" />

    <View
        android:id="@+id/line"
        style="@style/LineStyle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/image" />

</android.support.constraint.ConstraintLayout>

As you can find, contact_name is a TextView to right of image and top of guideline. Text of name can be too long therefore we have :

android:ellipsize="end"
android:maxLines="1"
android:maxWidth="248dp"

but setting maxWidth seems like a bad solution since device can be in landscape orientation.

What is your suggested approach to avoid conflicting between contact_name and phone_type TextView?

Upvotes: 1

Views: 276

Answers (3)

nikhil bansal
nikhil bansal

Reputation: 451

Try this. constraint contact_name on basis of phone_type.

<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:id="@+id/detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/contact_selector"
android:minHeight="64dp">

<FrameLayout
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="@+id/guideline"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline">

    <TextView
        android:id="@+id/image_text"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:background="@drawable/contact_circle"
        android:gravity="center"
        tools:text="A.R" />

</FrameLayout>

<TextView
    android:id="@+id/phone_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-light"
    android:paddingEnd="8dp"
    android:paddingRight="8dp"
    android:textSize="13sp"
    app:layout_constraintBottom_toBottomOf="@+id/image"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@+id/image"
    tools:text="Mobile" />

<TextView
    android:id="@+id/contact_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:maxWidth="248dp"
    android:paddingLeft="16dp"
    android:paddingStart="16dp"
    android:textSize="17sp"
    app:layout_constraintBottom_toTopOf="@+id/guideline"
    app:layout_constraintStart_toEndOf="@+id/image"
    app:layout_constraintEnd_toStartOf="@id/phone_type"
    tools:text="really long long long long long long long long long long name" />

<TextView
    android:id="@+id/phone_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingStart="16dp"
    android:textSize="13sp"
    app:layout_constraintStart_toStartOf="@+id/contact_name"
    app:layout_constraintTop_toTopOf="@+id/guideline"
    tools:text="+989121895634" />

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.5" />



<View
    android:id="@+id/line"
    style="@style/LineStyle"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@+id/image" />

Note - you can also replace frame layout with constraint layout.

Upvotes: 1

Pawel Laskowski
Pawel Laskowski

Reputation: 6346

You can constrain the end of the @id/contact_name to the start of the @id/phone_type and then set the horizontal bias to 0 to keep it aligned to the left and set app:layout_constrainedWidth="true" so the constraints are enforced when the text gets too long. Your TextView for @id/contact_name should look like this:

<TextView
    android:id="@+id/contact_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:paddingLeft="16dp"
    android:paddingStart="16dp"
    android:textSize="17sp"
    app:layout_constrainedWidth="true"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintBottom_toTopOf="@id/guideline"
    app:layout_constraintEnd_toStartOf="@id/phone_type"
    app:layout_constraintStart_toEndOf="@id/image"
    tools:text="Ali Rezaei" />

Another way would be to put these two Views in a horizontal chain and use spread_inside style for it and app:layout_constrainedWidth="true" for the @id/contact_name as well.

Upvotes: 2

Jeel Vankhede
Jeel Vankhede

Reputation: 12138

You can make your contact_name textview 0dp in width and set it's end constraint to the start of your phone_type textview and remove maxWidth attribute.

Check this updated layout :

<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:id="@+id/detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/contact_selector"
android:minHeight="64dp">

<FrameLayout
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="@+id/guideline"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline">

    <TextView
        android:id="@+id/image_text"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:background="@drawable/contact_circle"
        android:gravity="center"
        tools:text="A.R" />

</FrameLayout>

<TextView
    android:id="@+id/contact_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:paddingLeft="16dp"
    android:paddingStart="16dp"
    android:textSize="17sp"
    app:layout_constraintBottom_toTopOf="@+id/guideline"
    app:layout_constraintStart_toEndOf="@+id/image"
    app:layout_constraintEnd_toStartOf="@id/phone_type"
    tools:text="Ali Rezaei asdasdasdasdasdasdasdasd" />

<TextView
    android:id="@+id/phone_number"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingStart="16dp"
    android:textSize="13sp"
    app:layout_constraintStart_toStartOf="@+id/contact_name"
    app:layout_constraintTop_toTopOf="@+id/guideline"
    tools:text="+989121895634" />

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.5" />

<TextView
    android:id="@+id/phone_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-light"
    android:paddingEnd="8dp"
    android:paddingRight="8dp"
    android:textSize="13sp"
    app:layout_constraintBottom_toBottomOf="@+id/guideline"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline"
    tools:text="Mobile" />

<View
    android:id="@+id/line"
    style="@style/LineStyle"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@+id/image" />

</android.support.constraint.ConstraintLayout>

Let me know if it works !

Upvotes: 2

Related Questions