Search code examples
androidandroid-constraintlayout

ConstraintLayout - minimum width but maximised based on sibling


I'm trying to create a ConstraintLayout to do the following;

  1. on the left, a chessboard
  2. on the right, controls
  3. Chessboard should be a square, as big as the parent will allow
  4. however at least 1/3 of parent should be left for the controls
  5. importantly, if the squareness/size of the chessboard means MORE than 1/3 can be used for the control, then make the controls bigger.

Can this be done in XML? NB: obviously I can have two layouts, one for portrait, one for landscape. Happy to do 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestFragment">

    <!-- view_square should be;  -->
    <!-- be a square (nb: can *contain* a square eg a chessboard. The view itself can be rectangular -->
    <!-- as big as possible, eg in portrait mode width will determine height -->
    <!-- in landscape mode h will determine w -->
    <View
        android:id="@+id/view_square"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/view_other"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- this should be; -->
    <!-- width at a MINIMUM should be 1/3 of parent  -->
    <!-- width can be more than 1/3, if room is available after view_square's width is allocated -->
    <View
        android:id="@+id/view_other"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toRightOf="@id/view_square"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • First Create a guideline to constrain the right end of your chessboard.

    Then apply a dimension ratio of 1 to make your board a square and horizontal bias of 0 to keep it aligned to the left edge of parent.

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <View
            android:id="@+id/view_square"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintDimensionRatio="1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/guide_view_square"
            app:layout_constraintTop_toTopOf="parent" />
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guide_view_square"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.67"/>
    
        <View
            android:id="@+id/view_other"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintLeft_toRightOf="@id/view_square"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Now there are only two cases:

    • square fits between parents left and guideline leaving 33% for controls
    • squares size is determined by parents top and bottom, in that case horizontal bias of 0 forces it to stick to the left and controls take all remaining space