Guideline

public class Guideline
extends View

java.lang.Object
   ↳ View
     ↳ android.support.constraint.Guideline


Utility class representing a Guideline helper object for ConstraintLayout. Helper objects are not displayed on device (they are marked as View.GONE) and are only used for layout purposes. They only work within a ConstraintLayout.

A Guideline can be either horizontal or vertical:

  • Vertical Guidelines have a width of zero and the height of their ConstraintLayout parent
  • Horizontal Guidelines have a height of zero and the width of their ConstraintLayout parent

Positioning a Guideline is possible in three different ways:

  • specifying a fixed distance from the left or the top of a layout (layout_constraintGuide_begin)
  • specifying a fixed distance from the right or the bottom of a layout (layout_constraintGuide_end)
  • specifying a percentage of the width or the height of a layout (layout_constraintGuide_percent)

Widgets can then be constrained to a Guideline, allowing multiple widgets to be positioned easily from one Guideline, or allowing reactive layout behavior by using percent positioning.

See the list of attributes in ConstraintLayout.LayoutParams to set a Guideline in XML, as well as the corresponding ConstraintSet.setGuidelineBegin(int, int), ConstraintSet.setGuidelineEnd(int, int) and ConstraintSet.setGuidelinePercent(int, float) functions in ConstraintSet.

Example of a Button constrained to a vertical Guideline:

<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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/guideline"
            app:layout_constraintGuide_begin="100dp"
            android:orientation="vertical"/>

    <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            app:layout_constraintLeft_toLeftOf="@+id/guideline"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Summary

Public constructors

Guideline(Context context)
Guideline(Context context, AttributeSet attrs)
Guideline(Context context, AttributeSet attrs, int defStyleAttr)
Guideline(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

void setGuidelineBegin(int margin)

Set the guideline's distance from the top or left edge.

void setGuidelineEnd(int margin)

Set a guideline's distance to end.

void setGuidelinePercent(float ratio)

Set a Guideline's percent.

Inherited methods

Public constructors

Guideline

public Guideline (Context context)

Parameters
context Context

Guideline

public Guideline (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

Guideline

public Guideline (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Guideline

public Guideline (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

setGuidelineBegin

public void setGuidelineBegin (int margin)

Set the guideline's distance from the top or left edge.

Parameters
margin int: the distance to the top or left edge

setGuidelineEnd

public void setGuidelineEnd (int margin)

Set a guideline's distance to end.

Parameters
margin int: the margin to the right or bottom side of container

setGuidelinePercent

public void setGuidelinePercent (float ratio)

Set a Guideline's percent.

Parameters
ratio float: the ratio between the gap on the left and right 0.0 is top/left 0.5 is middle