Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE

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: enter image description here

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

So I'm wondering if there is some constraint which can realign baseline in the case if the dependent view is missing.

like image 612
novachevskyi Avatar asked Apr 07 '17 09:04

novachevskyi


People also ask

How can we solve missing constraints in constraint layout?

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.

What is baseline in constraint layout Android?

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.

What is 0dp in constraint layout?

Widgets dimension constraints Using 0dp , which is the equivalent of " MATCH_CONSTRAINT "

Can we use LinearLayout in ConstraintLayout?

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.


1 Answers

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> 
like image 151
Cheticamp Avatar answered Oct 03 '22 09:10

Cheticamp