androidkotlinandroid-constraintlayoutconstraintlayout-flowconstraintlayout-helper-widget-flow

How to get all children/referenced views inside a ConstraintLayout's Flow helper


I'm trying to adjust something (color, text, visibility or etc) to a group of Buttons inside a ConstraintLayout which are grouped using a Flow helper.

Since Flow or more accurately ConstraintLayout doesn't nest child Views inside itself using a ViewGroup but references their ids to achieve a more flat view tree, I failed to get a list of all children/referenced views of a certain Flow.

View

view

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent">

    <Button
        android:id="@+id/schedule_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/schedule" />

    <Button
        android:id="@+id/payments_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/payments" />

    <Button
        android:id="@+id/products_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/products" />

    <Button
        android:id="@+id/list_order_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/orders" />

    <Button
        android:id="@+id/new_order_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/new_order" />

    <Button
        android:id="@+id/categories_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/categories" />

    <Button
        android:id="@+id/subscriber_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/subscribers" />

    <Button
        android:id="@+id/account_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/accounts" />

    <Button
        android:id="@+id/reports_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/reports" />

    <Button
        android:id="@+id/workers_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/workers" />

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/buttons_flow"
        android:layout_width="347dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="32dp"
        android:orientation="horizontal"
        app:constraint_referenced_ids="new_order_button,list_order_button,products_button,categories_button,subscriber_button,workers_button,reports_button,payments_button,schedule_button,account_button"
        app:flow_horizontalGap="16dp"
        app:flow_maxElementsWrap="2"
        app:flow_verticalGap="16dp"
        app:flow_verticalStyle="packed"
        app:flow_wrapMode="aligned"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

I found some related questions:

However, those solutions are irrelevant for ConstraintLayout and especially Flow because Flow has no children and only some views are referenced to it.


Solution

  • Simple extension function

    fun Flow.referencedViews(): List<View> {
        val views = mutableListOf<View>()
        for (id in this.referencedIds) {
            val view = this.rootView.findViewById<View>(id)
            if (view != null) views.add(view)
        }
        return views
    }
    

    or a one-liner for the sake of brevity

    fun Flow.referencedViews() = referencedIds.toList().mapNotNull(rootView::findViewById)