user13567418
user13567418

Reputation:

Why does ViewPager Height match_parent Not Working?

I am trying to use match_parent height for my ViewPager to replace the fragment. But either layout_height="match_parent" or "wrap_content" not working. If i use them then my views become invisible.

Why does it only works when i do specify in dp? How do i make it to match_parent.

My XML Layout code: This is Fragment Layout replaced with FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Fragments.Frag.MyFragment">

        <com.google.android.material.tabs.TabLayout
            android:background="@drawable/toolbar"
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <androidx.viewpager.widget.ViewPager
        android:background="#f1f1f1"
        android:id="@+id/Viewpager"
        android:layout_width="match_parent"
        android:layout_height="600dp"/>

</LinearLayout>

My MainActivity which replaces above XML into FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white_light2"
    android:fitsSystemWindows="true">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                app:layout_scrollFlags="scroll|enterAlways"
                style="@style/customToolBar"
                android:id="@+id/toolbar1"
                app:title="Home">
            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:layout_marginBottom="?attr/actionBarSize"
            android:id="@+id/FragmentHolder"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.drawerlayout.widget.DrawerLayout>

Output i am getting as

My output of code, ViewPager not expanding

How can i avoid specifying ViewPager height in dp to take the entire screen space (match_parent)

Upvotes: 3

Views: 2065

Answers (3)

Saeed Moshfegh
Saeed Moshfegh

Reputation: 101

you need just add android:fillViewport="true" property in your NestedScrollView tag!

Upvotes: 5

Abdul
Abdul

Reputation: 887

So i have checked that and i am able to see the frame layout with full width and height. Might solve your problem.

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar1"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"
                app:title="Home" />
        </com.google.android.material.appbar.AppBarLayout>

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <FrameLayout
                android:id="@+id/FragmentHolder"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/black"
                android:backgroundTint="@color/black" />
        </androidx.core.widget.NestedScrollView>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.drawerlayout.widget.DrawerLayout>

Upvotes: 0

EpicPandaForce
EpicPandaForce

Reputation: 81539

You should do

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Fragments.Frag.MyFragment">

        <com.google.android.material.tabs.TabLayout
            android:background="@drawable/toolbar"
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <androidx.viewpager.widget.ViewPager
        android:background="#f1f1f1"
        android:id="@+id/Viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/> <!-- here -->

</LinearLayout>

Upvotes: 0

Related Questions