Stats

597 Contributors: 15 Thursday, April 13, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

ConstraintLayout

Introduction

ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. It is compatible with Android 2.3 (API level 9) and higher.

It allows you to create large and complex layouts with a flat view hierarchy. It is similar to RelativeLayout in that all views are laid out according to relationships between sibling views and the parent layout, but it's more flexible than RelativeLayout and easier to use with Android Studio's Layout Editor.

Syntax

  • ConstraintLayout

    • public void addView(View child, int index, ViewGroup.LayoutParams params)

    • public ConstraintLayout.LayoutParams generateLayoutParams(AttributeSet attrs)

    • public void onViewAdded(View view)

    • public void onViewRemoved(View view)

    • public void removeView(View view)

    • public void requestLayout()

    • protected boolean checkLayoutParams(ViewGroup.LayoutParams params)

    • protected ConstraintLayout.LayoutParams generateDefaultLayoutParams()

    • protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams params)

    • protected void onLayout(boolean changed, int left, int top, int right, int bottom)

    • protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

  • ConstraintLayout.LayoutParams

    • public void resolveLayoutDirection(int layoutDirection)

    • public void validate()

    • protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr)

Parameters

ParameterDetails
childThe View to be added to the layout
indexThe index of the View in the layout hierarchy
paramsThe LayoutParams of the View
attrsThe AttributeSet that defines the LayoutParams
viewThe View that has been added or removed
changedIndicates if this View has changed size or position
leftThe left position, relative to the parent View
topThe top position, relative to the parent View
rightThe right position, relative to the parent View
bottomThe bottom position, relative to the parent View
widthMeasureSpecThe horizontal space requirements imposed by the parent View
heightMeasureSpecThe vertical space requirements imposed by the parent View
layoutDirection-
a-
widthAttr-
heightAttr-

Remarks

At Google IO 2016 Google announced a new Android layout named ConstraintLayout.
Pay attention because currently, this layout is a Beta release.

Fore More About Constraint Layout:

https://codelabs.developers.google.com/codelabs/constraint-layout/index.html

Related Examples