Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep the margin when the referenced view is gone in ConstraintLayout

I have 2 TextViews as in the xml below. If I hide the textView2 at runtime, I lose the bottom margin. How can I keep the bottom margin between textView and parent to be 16dp when the textView2 is gone.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

  <TextView
      android:id="@+id/textView"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginBottom="16dp"
      android:layout_marginEnd="8dp"
      android:layout_marginStart="8dp"
      android:layout_marginTop="8dp"
      android:text="abc"
      app:layout_constraintBottom_toTopOf="@+id/textView2"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_chainStyle="packed"
      />

  <TextView
      android:id="@+id/textView2"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginBottom="32dp"
      android:layout_marginEnd="8dp"
      android:layout_marginStart="8dp"
      android:text="xyz"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/textView"
      app:layout_constraintVertical_chainStyle="packed"/>
</android.support.constraint.ConstraintLayout>
like image 922
pt2121 Avatar asked Oct 03 '17 16:10

pt2121


People also ask

What is barrier margin in Android?

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.

What is bias in constraint layout?

Bias, in terms of ConstraintLayout , means "if there is extra room, slide the widget in this direction along the axis". The default bias is 0.5, meaning that the widget is centered in the available space.

Can we use linear layout in ConstraintLayout?

You can create linear layouts now with ConstraintLayout by constraining the sides of each element with each other. The quick way of creating these layouts is to select all the views together and right click to center horizontally or vertically.


2 Answers

Use layout_goneMarginBottom:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

  <TextView
      android:id="@+id/textView"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginBottom="16dp"
      android:layout_marginEnd="8dp"
      android:layout_marginStart="8dp"
      android:layout_marginTop="8dp"
      android:text="abc"
      app:layout_goneMarginBottom="16dp"
      app:layout_constraintBottom_toTopOf="@+id/textView2"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_chainStyle="packed"
      />

  <TextView
      android:id="@+id/textView2"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginBottom="32dp"
      android:layout_marginEnd="8dp"
      android:layout_marginStart="8dp"
      android:text="xyz"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/textView"
      app:layout_constraintVertical_chainStyle="packed"/>
</android.support.constraint.ConstraintLayout>
like image 173
Benjamin Avatar answered Oct 19 '22 11:10

Benjamin


Below example helps to understand this concept

enter image description here

Below are the constraints for text3

 app:layout_goneMarginStart="100dp"
 app:layout_constraintStart_toEndOf="@id/text2"
 android:layout_marginStart="10dp"

since we had set start constraint with text2 for text3

  • when text2 is visible, android:layout_marginStart="10dp" will be considered
  • when text2 is gone,app:layout_goneMarginStart="100dp" will be considered as text3 start constraint is with text2. So, when startConstraint is gone it considers goneMarginStart.Similarly,if endConstraint is gone, goneMarginEnd will be considered as below

enter image description here

like image 3
Tarun A Avatar answered Oct 19 '22 13:10

Tarun A