Saturday, 16 January 2021

Understand The Barrier Practically In Android

  No comments

Hello Reader, 

In this post, I'm going to share my experience with the barrier and how it makes us(developers) life easier while handling the views based on visibility. So let first catch the problem and then the way to solve it. 


Problem: Suppose I have views like View A, View B, and View C.  So View C is aligned below View A and View B. But the visibility of both the views(A and B) is not fixed. In some cases, A will be VISIBLE and B will be GONE, and vice-versa.


Barrier image

Solution: I hope, you understand the situation and we need to resolve this only using the ConstraintLayout, not with the  LinearLayout OR RelativeLayout

Step1: Add a barrier in the layout and pass the app:barrierDirection as bottom OR top and in the app:constraint_referenced_ids passed the Ids i.e A and B.

app:barrierDirection="bottom"
app:constraint_referenced_ids="button2,button"

Step2: In View C, add a constraint and passed the barrier ID as below app:layout_constraintTop_toBottomOf="@id/barrier2"


Below is the sample code: 

No comments :

Post a Comment

Loading...