zihadrizkyef
zihadrizkyef

Reputation: 1941

Using match_parent in ConstraintLayout

Is it ok if we use match_parent in ConstraintLayout when we don't need any "constrainting"?

For example :

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

can be simplified to :

<View
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Upvotes: 1

Views: 2136

Answers (3)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Read Build a Responsive UI

The best way to create a responsive layout for different screen sizes is to use ConstraintLayout as the base layout in your UI. ConstraintLayout allows you to specify the position and size for each view according to spatial relationships with other views in the layout. This way, all the views can move and stretch together as the screen size changes.

Both layout's are right. Each layout has its own benefits but when it comes to complex, dynamic and responsive views you should always choose Constraint Layout.

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

OLD

    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"

NEW

    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"

Upvotes: 1

Bacho Kurtanidze
Bacho Kurtanidze

Reputation: 605

It's totally okay while using constraints is generally better idea if match_parent gives you same outcome you should go for it cause its less clutter. But in the end it's just the personal preference and doesn't really matter that much.

Upvotes: 1

Mike
Mike

Reputation: 1343

It defeats the purpose of constraint, you should probably use a LinearLayout or RelativeLayout if you are to use the 2nd approach. The goal of ConstraintLayout is to minimize sub levels / sub nests of layouts inside an XML (none of that multiple LinearLayouts or RelativeLayouts anymore instead we can use Groups or ConstraintLayout Flow)

Upvotes: 1

Related Questions