Search code examples
androidandroid-layoutandroid-studioandroid-viewandroid-constraintlayout

What does app:layout_goneMarginLeft="" do for views in constraintlayout?


How does the app:layout_goneMarginLeft and its variants affect the view arrangements in constraintlayout?


Solution

  • An example for this concept:

    Consider 2 TextViews with ids textView1 and textView2 where textView2 has a 0 dp constraint at the end of textView1.

    Case 1: When visibility of textView1 is VISIBLE, textView2 will be just right of textView1 with a 0 dp margin.

    <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="100dp">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/form_field_background"
            android:padding="5dp"
            android:text="TextView1"
            android:visibility="visible" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/form_field_background"
            android:padding="5dp"
            android:text="TextView2"
            app:layout_constraintStart_toEndOf="@+id/textView1"
            app:layout_goneMarginLeft="10dp"
            app:layout_goneMarginStart="10dp" />
    
    </android.support.constraint.ConstraintLayout>
    

    Result

    Enter image description here

    Case 2: When visibility of textView1 is GONE, textView2 will set its marginLeft to 10 dp since I have specified app:layout_goneMarginLeft="10dp"

    <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="100dp">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/form_field_background"
            android:padding="5dp"
            android:text="TextView1"
            android:visibility="gone" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/form_field_background"
            android:padding="5dp"
            android:text="TextView2"
            app:layout_constraintStart_toEndOf="@+id/textView1"
            app:layout_goneMarginLeft="10dp"
            app:layout_goneMarginStart="10dp" />
    
    </android.support.constraint.ConstraintLayout>
    

    Result

    Enter image description here