Guideline

public class Guideline
extends View

java.lang.Object
   ↳ android.view.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 setGuidelineBegin(int, int), setGuidelineEnd(int, int) and 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

Inherited constants

Inherited fields

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

Guideline (Context context)

Parameters
context Context

Guideline

Guideline (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

Guideline

Guideline (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Guideline

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

setGuidelineBegin

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

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

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