How does layout_width/height work together with min/max width/height Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How does layout_width/height work together with min/max width/height without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m trying to understand who “wins” when setting the following attributes in xml: 1. layout_width vs minWidth/maxWidth 2. layout_height vs minHeight/maxHeight

For example, the following code:

 <androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/pb_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/semi_transparent_black_for_pb_loader"
    android:elevation="20dp"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ProgressBar
        android:id="@+id/pb_loading"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

I want to understand what will be the ProgressBar size. in addition i found the value of style=”?android:attr/progressBarStyle”. attached here:

 <style name="Widget.ProgressBar">
    <item name="indeterminateOnly">true</item>
    <item name="indeterminateDrawable">@drawable/progress_medium_white</item>
    <item name="indeterminateBehavior">repeat</item>
    <item name="indeterminateDuration">3500</item>
    <item name="minWidth">48dip</item>
    <item name="maxWidth">48dip</item>
    <item name="minHeight">48dip</item>
    <item name="maxHeight">48dip</item>
    <item name="mirrorForRtl">false</item>
</style>

now, can someone tell me why when using wrap_content it’s seems that the size is determined according to minWidth, maxWidth, minHeight,maxHeight, whereas when setting layout_width and layout_height of the ProgressBar to “0dp”, then it’s “overcome” over the min/max and takes a lot of place from the screen?

Answer

When you set layout_width and layout_height to “0dp” it will respect the constraints that are defined. In your case:

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"

So it will take the whole space of the parent layout. This is similar as “match_parent”.

Where with “wrap_content” it will have the default size in this case 48dp as you found in the style definition.

We are here to answer your question about How does layout_width/height work together with min/max width/height - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji