Is there any way to realign baseline constraint for the TextView in case if dependent view visibility was set to GONE?
My layout code:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TITLE"
android:textColor="@color/black"
android:textSize="24sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/subtitle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBTITLE 1"
android:textSize="11sp"
app:layout_constraintBaseline_toBaselineOf="@+id/subtitle2"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/subtitleSpace"
app:layout_constraintRight_toRightOf="parent"/>
<android.support.v4.widget.Space
android:id="@+id/subtitleSpace"
android:layout_width="12dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/subtitle1"
app:layout_constraintRight_toLeftOf="@+id/subtitle2"/>
<TextView
android:id="@+id/subtitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="SUBTITLE 2"
android:textSize="14sp"
app:layout_constraintLeft_toRightOf="@+id/subtitleSpace"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title"/>
</android.support.constraint.ConstraintLayout>
In this case, my layout looks like this:

When I'm setting subtitle2 TextView visibility to GONE my layout will look like this:

So I'm wondering if there is some constraint which can realign baseline in the case if the dependent view is missing.
To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections. You can also visit the source code for the unit tests for this check to see additional scenarios.
Baseline alignmentAlign the text baseline of a view to the text baseline of another view. In figure 11, the first line of B is aligned with the text in A. To create a baseline constraint, right-click the text view you want to constrain and then click Show Baseline.
Widgets dimension constraints Using 0dp , which is the equivalent of " MATCH_CONSTRAINT "
Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout. However, learning the basics of LinearLayout and RelativeLayout is important before trying to understand how to use it with ConstraintLayout.
Alternate margins can be specified for GONE widgets (see Margins when connected to a GONE widget), but I am not aware of such an alternative for baselines.
One way to satisfy your requirement is to specify a 0dp invisible TextView that has the same characterisics of the GONE TextView that is placed next to that same view. subtitle1 and subtitle2 would tie to the baseline of this new TextView. Even though the new TextView doesn't show on the screen and takes zero width, it still maintains a baseline.
Now, when subtitle2 is made GONE, subtitle1 will move to the center but maintain its vertical position. (Specifying 0dp and invisible with null text is redundant but makes it clear that the widget should not appear.)
Here is the XML with these changes:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TITLE"
android:textColor="#FF000000"
android:textSize="24sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/subtitle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBTITLE 1"
android:textSize="11sp"
app:layout_constraintBaseline_toBaselineOf="@+id/viewForBaseline"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/subtitleSpace"
app:layout_constraintRight_toRightOf="parent" />
<android.support.v4.widget.Space
android:id="@+id/subtitleSpace"
android:layout_width="12dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/subtitle1"
app:layout_constraintRight_toLeftOf="@+id/subtitle2" />
<TextView
android:id="@+id/subtitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="SUBTITLE 2"
android:textSize="14sp"
android:visibility="gone"
app:layout_constraintBaseline_toBaselineOf="@+id/viewForBaseline"
app:layout_constraintLeft_toRightOf="@+id/subtitleSpace"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title" />
<TextView
android:id="@+id/viewForBaseline"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textSize="14sp"
android:visibility="invisible"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title" />
</android.support.constraint.ConstraintLayout>
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