Sudar Nimalan
Sudar Nimalan

Reputation: 3992

Issue in ConstraintLayout implementation

I expect layout_constraintDimensionRatio to work with layout_constraintWidth_percent. But not working as expected. For example create a square view, which width is half of parent width.

    <Button
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constrainedHeight="true"
    app:layout_constrainedWidth="true"
    app:layout_constraintDimensionRatio="H,1:1"
    app:layout_constraintWidth_default="percent"
    app:layout_constraintWidth_percent="0.5" />

I am reading following from android.support.constraint.ConstraintLayout

    private void setChildrenConstraints() {
    ...............

                if(layoutParams.dimensionRatio != null) {
                    widget.setDimensionRatio(layoutParams.dimensionRatio);
                }

                widget.setHorizontalWeight(layoutParams.horizontalWeight);
                widget.setVerticalWeight(layoutParams.verticalWeight);
                widget.setHorizontalChainStyle(layoutParams.horizontalChainStyle);
                widget.setVerticalChainStyle(layoutParams.verticalChainStyle);
                widget.setHorizontalMatchStyle(layoutParams.matchConstraintDefaultWidth, layoutParams.matchConstraintMinWidth, layoutParams.matchConstraintMaxWidth, layoutParams.matchConstraintPercentWidth);
                widget.setVerticalMatchStyle(layoutParams.matchConstraintDefaultHeight, layoutParams.matchConstraintMinHeight, layoutParams.matchConstraintMaxHeight, layoutParams.matchConstraintPercentHeight);

    }

}

I think, setDimensionRatio should be called after setVerticalWeight for this. Is this a bug inside ConstraintLayout. Or Am I missing any thing?

Upvotes: 0

Views: 2039

Answers (3)

Sudar Nimalan
Sudar Nimalan

Reputation: 3992

with the help of Dipali's answer I could make the design as I want.

<Button
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="H,1:1"
    app:layout_constraintRight_toLeftOf="@+id/guide_line"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.constraint.Guideline
    android:id="@+id/guide_line"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5" />

But what I found is : layout_constraintDimensionRatio will not work with layout_constraintWidth_percent or layout_constraintHorizontal_chainStyle etc.

Upvotes: 0

Dipali Shah
Dipali Shah

Reputation: 3798

If you want your View to be 50% of width Add Guideline with orientation="vertical"

E.g.

 <ImageView
        android:id="@+id/imageView7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toStartOf="@+id/guideline4"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"

        app:srcCompat="@drawable/attach_audio" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

enter image description here Imageview of 50% width of parent

Upvotes: 1

Anthonyeef
Anthonyeef

Reputation: 2675

Can you post more about your ConstraintLayout attributes? According to what I know and the official document,

You can also define one dimension of a widget as a ratio of the other one. In order to do that, you need to have at least one constrained dimension be set to 0dp (i.e., MATCH_CONSTRAINT), and set the attribute layout_constraintDimensionRatio to a given ratio.

Have you set the layout_width to 0dp ?

Upvotes: 0

Related Questions