Ted Henry
Ted Henry

Reputation: 1492

Android layout_width="match_parent" in generated constraint layout?

When I create a new Android app project with Android Studio, the file activity_main.xml contains the following. Why does the TextView have layout_width and layout_height attributes with values wrap_content? I thought that if the view is being laid out with constraints, the layout_width and layout_height are supposed to have values 0dp.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Upvotes: 2

Views: 818

Answers (2)

Kartika Vij
Kartika Vij

Reputation: 211

Values of the attributes layout_width and layout_height totally depends on your requirement For more information check this link:-

For more information:- https://developer.android.com/reference/android/support/constraint/ConstraintLayout

Upvotes: 1

Denysole
Denysole

Reputation: 4061

It's not true, depending on you situation you could use either wrap_content or 0dp

The dimension of the widgets can be specified by setting the android:layout_width and android:layout_height attributes in 3 different ways:

  • Using a specific dimension (either a literal value such as 123dp or a Dimension reference)

  • Using WRAP_CONTENT, which will ask the widget to compute its own size

  • Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"

And also

When a dimension is set to MATCH_CONSTRAINT, the default behavior is to have the resulting size take all the available space.

More details here ConstraintLayout

Upvotes: 0

Related Questions