Skip to content

Instantly share code, notes, and snippets.

@mreichelt
Last active April 25, 2023 09:00
Show Gist options
  • Save mreichelt/2e8729021efc92ff8ee0e35b71cab3d2 to your computer and use it in GitHub Desktop.
Save mreichelt/2e8729021efc92ff8ee0e35b71cab3d2 to your computer and use it in GitHub Desktop.
Example of using ConstraintLayout: The growing view will be only as large as it needs to be - unless it grows too large. Then it will only be as large as the guideline allows.
<?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.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.3" />
<TextView
android:id="@+id/growing_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintHeight_default="wrap"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:text="This view wraps to its content height until it grows bigger than the guideline allows. Try to add more text to see that it will only grow until it reaches the guideline. Awesome! :-D" />
<TextView
android:id="@+id/more_content"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/growing_view"
tools:text="More content" />
</android.support.constraint.ConstraintLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment