0

I am trying to have something which displays 2 views side by side if there is enough space, or display them vertically on smaller screens like phones in potrait

I have this:

<?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">

    <View
        android:id="@+id/test_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#4f00"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintHeight_max="500dp"
        app:layout_constraintWidth_max="500dp"
        app:layout_constraintWidth_min="150dp" />

    <View
        android:id="@+id/test_view2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#40f0" />

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/random_flow"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="16dp"
        android:orientation="horizontal"
        app:constraint_referenced_ids="test_view, test_view2"
        app:flow_horizontalGap="16dp"
        app:flow_horizontalStyle="packed"
        app:flow_maxElementsWrap="1"
        app:flow_verticalGap="16dp"
        app:flow_verticalStyle="packed"
        app:flow_wrapMode="chain"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

The views are shown in vertical orientation in all screen sizes even when I have specified orientation="horizontal". Looks like they are always wrapping even when there is space to display them horizontally?

I also need test_view (red) to take as much space as available within layout_constraintWidth_max and layout_constraintWidth_min. Looks like it is always using the min.

landscape portrait

hoshiKuzu
  • 866
  • 1
  • 12
  • 30

1 Answers1

0

1.it seems 0dp for width and height of constraintlayout.helper.widget.Flow is not working together, so you have to change 1 of them to match_parent or just width to wrap_content. (i recommend changing to match_parent. because that's what u want)

2.change the flow_wrapMode from: chain to chain2.

I put the changed code here : if you change the width of view1(the pink one). you see orientation will change

<View
    android:id="@+id/test_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#4f00"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintHeight_max="500dp"
    app:layout_constraintWidth_max="500dp"
    app:layout_constraintWidth_min="150dp" />

<View
    android:id="@+id/test_view2"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="#40f0" />

<androidx.constraintlayout.helper.widget.Flow
    android:id="@+id/random_flow"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:orientation="horizontal"
    app:constraint_referenced_ids="test_view, test_view2"
    app:flow_horizontalGap="16dp"
    app:flow_horizontalStyle="packed"
    app:flow_maxElementsWrap="1"
    app:flow_verticalGap="16dp"
    app:flow_verticalStyle="packed"
    app:flow_wrapMode="chain2"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="parent" />
  • The red square still doesn't fill the max space available, but it does when app:flow_wrapMode="none". According to this https://stackoverflow.com/a/56106542/1067596, it looks like a hardcoded width value or wrap_content is required in chain mode. So, I gave up. The other way to do this would be to set orientation to vertical or horizontal based on breakpoints, with wrapMode="none" – hoshiKuzu Mar 13 '22 at 14:03