Search code examples
androidandroid-constraintlayout

ConstraintLayout - Spead horizontally with weight 1, ratio vertically


I am trying to make 2 views chain horizontally, so they both take 50% of screen and make their height same as width so they are square. Currently I have this, which doesn't seem to work:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/a"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#F00"
        app:layout_constraintDimensionRatio="H,1:1"
        app:layout_constraintEnd_toStartOf="@+id/b"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/b"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#0F0"
        app:layout_constraintDimensionRatio="H,1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/a"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

Solution

  • Silambarasan's answer didn't actually do what I wanted to do but his use of Guideline helped me hint helped me get the right answer

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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">
    
        <android.support.constraint.Guideline
            android:id="@+id/mid_guide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.50" />
    
        <View
            android:id="@+id/a"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#F00"
            app:layout_constraintDimensionRatio="H,1:1"
            app:layout_constraintEnd_toEndOf="@id/mid_guide"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <View
            android:id="@+id/b"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#0F0"
            app:layout_constraintDimensionRatio="H,1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@id/mid_guide"
            app:layout_constraintTop_toTopOf="parent" />
    
    
    </android.support.constraint.ConstraintLayout>