How does the app:layout_goneMarginLeft
and its variants affect the view arrangements in constraintlayout?
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>
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With