Vassili Kurman
Vassili Kurman

Reputation: 13

How to set image width to take half the space in ConstraintLayout

I need a help on setting up ImageView width to take 50% of the space in the parent ConstraintLayout. Here is a part of the layout file that is responsible for image display:

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/poster_iv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintHorizontal_bias="0.5"
            android:layout_marginEnd="@dimen/default_margin_sides"
            android:layout_marginStart="@dimen/default_margin_sides"
            android:layout_marginTop="@dimen/default_margin_top_bottom"
            android:contentDescription="@string/iv_poster_description"
            android:adjustViewBounds="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>
</ScrollView>

In the case above it takes all width available

Upvotes: 1

Views: 1448

Answers (1)

Nero
Nero

Reputation: 1048

You should be able to use percentages to define the width and height (but only width in this case). Can you please add the following attributes to your ImageView

app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent=".5"

This should center your ImageView to take half of the space as you've requested.

Like this:
enter image description here

Upvotes: 2

Related Questions