Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 465
Mohammad Nasrollahi Avatar asked Dec 01 '22 10:12

Mohammad Nasrollahi


2 Answers

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

like image 155
Varad Mondkar Avatar answered Dec 04 '22 02:12

Varad Mondkar


When you constrain, for example, a view, B, to another view, A, and at some point in your logic/code you change the visibility of the view A to view.GONE, if you use app:layout_goneMarginLeft="" for example, it will keep the margins or like said in the documentation:

you can also indicate a different margin value to be used

You can find an example here.

like image 44
Belbahar Raouf Avatar answered Dec 04 '22 02:12

Belbahar Raouf