1

Im facing an issue with ConstraintLayout.

I have a view which has 3 textviews T1, T2 and T3.

Precondition: T1,T2 is having 4dp margin top.

Problem: T3 needs to be top should be aligned to topmost view. Condition is T1 will be visible or gone based on certain condition.
If T1 is visible the margintop will be 8dp and when T1 is not visible T2 will have 12dp margintop.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="T1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:text="T2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />


    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="T3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/textView" />


</android.support.constraint.ConstraintLayout>

Is this possible by XML or I need to configure programmatically?

3 Answers3

0

I don't know exactly your use case,but you can achieve this setting view to invisible instead of gone.

View.GONE This view is invisible, and it doesn't take any space for layout purposes.

View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.

Chan Myae Aung
  • 487
  • 3
  • 13
0

You can't achieve this only with XML, what you did is create a screen using constraint layout.
If you want to add some logic to change the position of some view under certain conditions you will have to write it programmatically.
Here is a good thread about the subject.

Tamir Abutbul
  • 6,472
  • 7
  • 16
  • 44
0

You can use layout_goneMarginTop to have a different margin when your view is GONE.

You can also use SequenceLayout for better flexibility on managing your layout. Use visibilityElement on each Span you want to be gone when your related view is gone.