Paolo Colombo
Paolo Colombo

Reputation: 239

ConstraintLayout in Android Studio not match_parent

I'm trying to using a ConstraintLayout in Android Studio and I'm building a fragment in this way, in particular I use android:layout_height="match_parent".

Layout of fragment

Here's my code.

<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"
android:background="@android:color/holo_blue_light"
tools:context="com.saffru.colombo.soccerstats.match.PrePartitaFragment">

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Prepartita"
    android:textColor="@android:color/black"
    android:textSize="24sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toStartOf="@+id/txtdate"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/txtdate"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/txtdate"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp"
    android:focusable="false"
    android:hint="Tocca per inserire la data*"
    android:textSize="20sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView3" />


<ListView
    android:id="@+id/ListView01"
    android:layout_width="match_parent"
    android:layout_height="300sp"
    android:layout_marginEnd="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginStart="10dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/et_avversario" />

<EditText
    android:id="@+id/et_avversario"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginStart="10dp"
    android:ems="10"
    android:hint="Avversario"
    android:inputType="textPersonName|textCapSentences"
    app:layout_constraintEnd_toStartOf="@+id/ListView01"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/ListView01"
    app:layout_constraintTop_toBottomOf="@+id/et_luogo" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:text="In casa"
    app:layout_constraintTop_toBottomOf="@+id/txtdate" />

<EditText
    android:id="@+id/et_luogo"
    android:layout_width="260dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:ems="10"
    android:hint="Luogo"
    android:inputType="textPersonName|textCapSentences"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/checkBox"
    app:layout_constraintTop_toBottomOf="@+id/txtdate" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16sp"
    android:layout_marginEnd="16sp"
    android:layout_marginRight="16sp"
    android:background="@color/colorAccent"
    android:src="@drawable/ic_check"
    android:textColor="#fff"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

But when I run my application I see that the layout is not "match_parent".

enter image description here

and the activity is slightly scrollable.

Any ideas to resolve this problem? Here's my activity xml.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout      
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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".match.PartitaSpinnerActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_weight="1"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:title="@string/app_name">

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

Upvotes: 2

Views: 2476

Answers (2)

Neeraj Singh
Neeraj Singh

Reputation: 668

There is a type of constraint which breaks the scroll function:

Remove bellow attributes from other view.

app:layout_constraintBottom_toBottomOf="parent"

Happy Coding..

Upvotes: 0

Levi Moreira
Levi Moreira

Reputation: 12007

Try adding:

android:fillViewport="true"

to your NestedScrollView

<android.support.v4.widget.NestedScrollView
    android:id="@+id/container"
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

It's not usual to have the Fragment container to be a NestedScrollView. Normally a FrameLayout is used. If you want it to be scrollable, wrap it with the NestedScrollView.

Upvotes: 6

Related Questions