Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Mustafa Ogün Öztürk
Mustafa Ogün Öztürk
3,468 Points

How to get text views vertically even spaced ?

Hi all

I have simple ConstraintLayout as below

<ImageView
    android:id="@+id/catPicImageView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/cat" />

<TextView
    android:id="@+id/helloTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="@string/hello_katze"
    android:textSize="24sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/catPicImageView" />

<TextView
    android:id="@+id/catNameTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/name"
    android:textSize="16sp"
    app:layout_constraintBottom_toTopOf="@+id/CatAgeTextView"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/helloTextView" />

<TextView
    android:id="@+id/CatAgeTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/age"
    android:textSize="16sp"
    app:layout_constraintBottom_toTopOf="@+id/catBehaviourTextView"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/catNameTextView" />

<TextView
    android:id="@+id/catBehaviourTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/behaviour"
    android:textSize="16sp"
    app:layout_constraintBottom_toTopOf="@+id/button"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/CatAgeTextView" />

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/click_to_see_details"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

I want the last 3 TextView's to be evenly spaced. How can i make this happen ?

Thanks

1 Answer

Ashraf Rahman
Ashraf Rahman
6,111 Points

Try placing the TextViews in a LinearLayout(vertical) then assign a weight of 1 to each so it can be even, or in the linear layout change the weight sum to 100 and each of the TextViews weight to 25 (another method).

p.s this should be within the constraints layout NOT replacing the constraints layout.

Mustafa Ogün Öztürk
Mustafa Ogün Öztürk
3,468 Points

I think, using LinearLayout/RelativeLayout in a ConstraintLayout is not a good practice since ConstraintLayout is designed to reduce View complexity. ConstraintLayout has some properties to do what i want to do and they are called "Chains". I am working on it to get some information. Thanks for the answer though.