Sourabh Pal
Sourabh Pal

Reputation: 104

Text view not aligning to the guideline in constraint layout

I have the following Constraint Layout:

`<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp">

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

        <android.support.constraint.Guideline
            android:id="@+id/vertical_guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.6" />

        <TextView
            android:id="@+id/date_tv"
            android:layout_width="wrap_content"
            android:layout_height="16dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="2 hours ago" />

        <TextView
            android:id="@+id/source_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:textStyle="bold"
            app:layout_constraintLeft_toRightOf="@id/date_tv"
            app:layout_constraintTop_toTopOf="@id/date_tv"
            tools:text="BBC News" />

        <TextView
            android:id="@+id/headline_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="32dp"
            android:maxLines="2"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/date_tv"
            app:layout_constraintRight_toLeftOf="@id/vertical_guideline"
            tools:text="Apple admits slowing down older iphones" />

    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>`

It has the following output:

Layout output of the above code

Now what I want is that the headline text view should only be as wide as the guideline and should move to the next line when the text is long. Which is not happening. Can someone please help me out with this?

Upvotes: 8

Views: 2940

Answers (2)

Archie G. Qui&#241;ones
Archie G. Qui&#241;ones

Reputation: 13668

You are using android:width=wrap_content for headline_tv which prioritizes the actually width of the textview. Use 0dp so it would match constraint.

Upvotes: 1

Cheticamp
Cheticamp

Reputation: 62831

Try setting the width to 0dp (match_constraints) as follows:

    <TextView
        android:id="@+id/headline_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="32dp"
        android:maxLines="2"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/date_tv"
        app:layout_constraintRight_toLeftOf="@id/vertical_guideline"
        tools:text="Apple admits slowing down older iphones" />

Upvotes: 13

Related Questions