So I have a constraintLayout like this
<ConstraintLayout>
<TextView
android:id="@+id/text_view"
app:layout_constraintEnd_toStartOf="@+id/view1" />
<View
android:id="@+id/view1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<View
android:id="@+id/view2"
aapp:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ConstraintLayout>
Currently the textview's end is at the start of view1, however, when view1's visibility is gone, I want the end of textview to the start of view2, otherwise the long string may cover view2. Is there any way to set the constraint to two views or is there any better solution for this?
Open the layout file (activity_main. xml) in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click LinearLayout and then choose Convert layout to ConstraintLayout from the context menu.
More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.
Groups is used when you want to handle visibility or motion with all its view. The Group in ConstraintLayout is just a loose association of views. Guideline: A guideline is a visual guide that will not be seen at runtime that is used to align other views.
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.
You can use a Barrier to constraint to multiple views.
<ConstraintLayout>
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
app:barrierDirection="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="view1,view2"/>
<TextView
android:id="@+id/text_view"
app:layout_constraintEnd_toStartOf="@id/barrier" />
<View
android:id="@+id/view1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<View
android:id="@+id/view2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ConstraintLayout>
The textview will be constraint to the view that is more to the left
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