Search code examples
androidandroid-layoutandroid-viewpagerandroid-tabactivity

Android Tab Layout is not showing the view


Hi I have the following issue , when pressing a tab icon the view is not shown, i can see the class is being loaded. I can see the that in the console log. I believe the issue is related the the xml as i have moved the relative layout main_content below the view pager and when the icon is pressed in the tab layout, the tab fragment content is shown but the main content views don't attach then which is a problem.

My Layout:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="@dimen/custom_tab_layout_height"
                app:tabGravity="fill"
                app:tabMode="fixed" />

            <RelativeLayout
                android:id="@+id/main_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </RelativeLayout>


            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </android.support.v4.view.ViewPager>

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

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


    <RelativeLayout
        android:id="@+id/drawer_pane"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <!-- Profile box -->

        <RelativeLayout
            android:id="@+id/profile_box"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/material_blue_grey_800"
            android:gravity="center_vertical">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:background="@drawable/ic_mail_black_24dp" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@+id/icon"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Phong"
                    android:textColor="#fff"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="PTIT HCM"
                    android:textColor="#fff"
                    android:textSize="16sp" />
            </LinearLayout>
        </RelativeLayout>

        <!-- Navigation Listview -->

        <ListView
            android:id="@+id/nav_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/profile_box"
            android:background="#ffffff"
            android:choiceMode="singleChoice">

        </ListView>
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

Solution

  • Since you are using CoordinatorLayout, Let's use that NavigationDrawer too.

    Here what you should exactly do (StandardWay):

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.v4.widget.NestedScrollView
                android:id="@+id/scroll"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
                <!-- Your Contents -->
    
            </android.support.v4.widget.NestedScrollView>
    
    
            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimaryDark"
                    app:layout_scrollFlags="scroll|enterAlways"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabGravity="fill"
                    app:tabMode="fixed" />
    
                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true"
                    app:contentScrim="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                    <android.support.v4.view.ViewPager
                        android:id="@+id/pager"
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent"
                        android:layout_below="@id/tab_layout" />
    
                </android.support.design.widget.CollapsingToolbarLayout>
    
            </android.support.design.widget.AppBarLayout>
    
        </android.support.design.widget.CoordinatorLayout>
    
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/header"
            app:menu="@menu/drawer" />
        <!-- navigationdrawer menu -->
    
    </android.support.v4.widget.DrawerLayout>
    

    Not sure if this is a good implementation :

    <RelativeLayout
            android:id="@+id/drawer_pane"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start">
    

    especially, with this one:

    <!-- Navigation Listview -->
    
            <ListView
                android:id="@+id/nav_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/profile_box"
                android:background="#ffffff"
                android:choiceMode="singleChoice" />
    

    Here you learn more about using this CoordinatorLayout with contents and mastering it:

    http://saulmm.github.io/mastering-coordinator/