Barrier

public class Barrier
extends View

java.lang.Object
   ↳ android.view.View
     ↳ androidx.constraintlayout.widget.Barrier


Added in 1.1

A Barrier references multiple widgets as input, and creates a virtual guideline based on the most extreme widget on the specified side. For example, a left barrier will align to the left of all the referenced views.

Example

Let's have two buttons, @id/button1 and @id/button2. The constraint_referenced_ids field will reference them by simply having them as comma-separated list:
     <androidx.constraintlayout.widget.Barrier
              android:id="@+id/barrier"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              app:barrierDirection="start"
              app:constraint_referenced_ids="button1,button2" />
     
     

With the barrier direction set to start, we will have the following result:

Reversely, with the direction set to end, we will have:

If the widgets dimensions change, the barrier will automatically move according to its direction to get the most extreme widget:

Other widgets can then be constrained to the barrier itself, instead of the individual widget. This allows a layout to automatically adapt on widget dimension changes (e.g. different languages will end up with different length for similar worlds).

GONE widgets handling

If the barrier references GONE widgets, the default behavior is to create a barrier on the resolved position of the GONE widget. If you do not want to have the barrier take GONE widgets into account, you can change this by setting the attribute barrierAllowsGoneWidgets to false (default being true).

Summary

Constants

int BOTTOM

Bottom direction constant

int END

End Barrier constant

int LEFT

Left direction constant

int RIGHT

Right direction constant

int START

Start direction constant

int TOP

Top direction constant

Inherited constants

Fields

protected HashMap<Integer, String> mMap

Inherited fields

Public constructors

Barrier(Context context)
Barrier(Context context, AttributeSet attrs)
Barrier(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

void addView(View view)

Add a view to the helper.

boolean allowsGoneWidget()

This method is deprecated. This method should be called getAllowsGoneWidget such that allowsGoneWidget can be accessed as a property from Kotlin; . Use getAllowsGoneWidget() instead.

boolean containsId(int id)
boolean getAllowsGoneWidget()

Find if this barrier supports gone widgets.

int getMargin()

Returns the barrier margin

int[] getReferencedIds()

Helpers typically reference a collection of ids

int getType()

Get the barrier type (Barrier.LEFT, Barrier.TOP, Barrier.RIGHT, Barrier.BOTTOM, Barrier.END, Barrier.START)

int indexFromId(int id)
void loadParameters(ConstraintSet.Constraint constraint, HelperWidget child, ConstraintLayout.LayoutParams layoutParams, SparseArray<ConstraintWidget> mapIdToWidget)
int removeView(View view)

Remove a given view from the helper.

void resolveRtl(ConstraintWidget widget, boolean isRtl)
void setAllowsGoneWidget(boolean supportGone)
void setDpMargin(int margin)

Set a margin on the barrier

void setMargin(int margin)

Set the barrier margin

void setReferencedIds(int[] ids)

Helpers typically reference a collection of ids

void setTag(int key, Object tag)
void setType(int type)

Set the barrier type (Barrier.LEFT, Barrier.TOP, Barrier.RIGHT, Barrier.BOTTOM, Barrier.END, Barrier.START)

void updatePostConstraints(ConstraintLayout container)
void updatePreDraw(ConstraintLayout container)
void updatePreLayout(ConstraintWidgetContainer container, Helper helper, SparseArray<ConstraintWidget> map)

Protected methods

View[] getViews(ConstraintLayout layout)
void onAttachedToWindow()

Inherited methods

Constants

BOTTOM

public static final int BOTTOM

Bottom direction constant

Constant Value: 3 (0x00000003)

END

public static final int END

End Barrier constant

Constant Value: 6 (0x00000006)

LEFT

public static final int LEFT

Left direction constant

Constant Value: 0 (0x00000000)

public static final int RIGHT

Right direction constant

Constant Value: 1 (0x00000001)

START

public static final int START

Start direction constant

Constant Value: 5 (0x00000005)

TOP

public static final int TOP

Top direction constant

Constant Value: 2 (0x00000002)

Fields

mMap

protected HashMap<Integer, String> mMap

Public constructors

Barrier

public Barrier (Context context)

Parameters
context Context

Barrier

public Barrier (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

Barrier

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

addView

public void addView (View view)

Add a view to the helper. The referenced view need to be a child of the helper's parent. The view also need to have its id set in order to be added.

allowsGoneWidget

public boolean allowsGoneWidget ()

This method is deprecated.
This method should be called getAllowsGoneWidget such that allowsGoneWidget can be accessed as a property from Kotlin; . Use getAllowsGoneWidget() instead.

Find if this barrier supports gone widgets.

Returns
boolean true if this barrier supports gone widgets, otherwise false

containsId

public boolean containsId (int id)

Parameters
id int

Returns
boolean

getAllowsGoneWidget

public boolean getAllowsGoneWidget ()

Find if this barrier supports gone widgets.

Returns
boolean true if this barrier supports gone widgets, otherwise false

getMargin

public int getMargin ()

Returns the barrier margin

Returns
int the barrier margin (in pixels)

getReferencedIds

public int[] getReferencedIds ()

Helpers typically reference a collection of ids

Returns
int[] ids referenced

getType

public int getType ()

Get the barrier type (Barrier.LEFT, Barrier.TOP, Barrier.RIGHT, Barrier.BOTTOM, Barrier.END, Barrier.START)

Returns
int

indexFromId

public int indexFromId (int id)

Parameters
id int

Returns
int

loadParameters

public void loadParameters (ConstraintSet.Constraint constraint, 
                HelperWidget child, 
                ConstraintLayout.LayoutParams layoutParams, 
                SparseArray<ConstraintWidget> mapIdToWidget)

Parameters
constraint ConstraintSet.Constraint

child HelperWidget

layoutParams ConstraintLayout.LayoutParams

mapIdToWidget SparseArray

removeView

public int removeView (View view)

Remove a given view from the helper.

Returns
int index of view removed

resolveRtl

public void resolveRtl (ConstraintWidget widget, 
                boolean isRtl)

Parameters
widget ConstraintWidget

isRtl boolean

setAllowsGoneWidget

public void setAllowsGoneWidget (boolean supportGone)

Parameters
supportGone boolean

setDpMargin

public void setDpMargin (int margin)

Set a margin on the barrier

Parameters
margin int: in dp

setMargin

public void setMargin (int margin)

Set the barrier margin

Parameters
margin int: in pixels

setReferencedIds

public void setReferencedIds (int[] ids)

Helpers typically reference a collection of ids

Parameters
ids int

Returns
void ids referenced

setTag

public void setTag (int key, 
                Object tag)

Parameters
key int

tag Object

setType

public void setType (int type)

Set the barrier type (Barrier.LEFT, Barrier.TOP, Barrier.RIGHT, Barrier.BOTTOM, Barrier.END, Barrier.START)

Parameters
type int

updatePostConstraints

public void updatePostConstraints (ConstraintLayout container)

Parameters
container ConstraintLayout

updatePreDraw

public void updatePreDraw (ConstraintLayout container)

Parameters
container ConstraintLayout

updatePreLayout

public void updatePreLayout (ConstraintWidgetContainer container, 
                Helper helper, 
                SparseArray<ConstraintWidget> map)

Parameters
container ConstraintWidgetContainer

helper Helper

map SparseArray

Protected methods

getViews

protected View[] getViews (ConstraintLayout layout)

Parameters
layout ConstraintLayout

Returns
View[]

onAttachedToWindow

protected void onAttachedToWindow ()