Hendra Anggrian
Hendra Anggrian

Reputation: 5858

Height of a child view is exceeding height of CoordinatorLayout

Below are the xml code of my MainActivity, I think it's pretty standard:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:tabIndicatorColor="@android:color/white"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

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

    <android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

However, I got this result in preview:

enter image description here

Just for the sake of testing, I created an xml which contains text from A to N:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="AAA"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="BBB"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="CCC"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="DDD"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="EEE"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="FFF"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="GGG"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="HHH"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="III"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="JJJ"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="KKK"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="LLL"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="MMM"
            android:textSize="32dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="NNN"
            android:textSize="32dp"/>
    </LinearLayout>
</ScrollView>

And here's the result on my Nexus 6, scrolled to the very bottom:

enter image description here

As you can see, the height of ViewPager is exceeding its parent layout causing some elements below clipped, and I wonder why? I'm using API level 23, it might be the root cause but I can't lower it down because I'm using a library that highly depends on API 23.

Can someone clarify is this a bug? I can use paddingBottom or layout_MarginBottom as a quick solution but I don't think it's elegant.

Upvotes: 2

Views: 1569

Answers (2)

Hendra Anggrian
Hendra Anggrian

Reputation: 5858

I solved my own problem by using android.support.v4.widget.NestedScrollView, not regular ScrollView.

Upvotes: 1

Varun Kumar
Varun Kumar

Reputation: 1261

No, it's not a bug. Its the function of "app:layout_behavior" attribute in your view pager. Don't worry its pretty normal thing and it works perfectly fine when you run it on any Android device.

Upvotes: 0

Related Questions