lee huang lee huang - 5 months ago 26
Android Question

Need to include Tabs with Nav Drawer

My main activity layout uptil now looks like this:

<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<include
android:id="@+id/toolbar"
layout="@layout/toolbar" >
</include>

</LinearLayout>

<FrameLayout
android:id="@+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>

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

<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.v4.view.ViewPager>

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


</LinearLayout>

<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="android.mybitchinapp.com.testgui.activity.FragmentDrawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" >
</fragment>

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


I need to include a TabLayout in order to provide tabs to the app. I cant figure out how to include it. I tried to add an AppBarLayout (which would contain TabLayout) but it leads to app crash. So its obviously not a solution. How do I edit it to make it have a TabLayout too?
Final product required is :
enter image description here

Mine looks like this:
enter image description here

That is, it has fragments and tha nav drawer. The only missing this is the actual tabs.

Answer

Your xml layout seems confusing. So I have edited your xml as a basic idea to achieve what you need as your given screenshot.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.support.design.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <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">

                <include
                   android:id="@+id/toolbar"
                   layout="@layout/toolbar" >
                </include>

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </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>

            <fragment 
                android:id="@+id/fragment_navigation_drawer"          
                android:layout_width="@dimen/nav_drawer_width"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                app:layout="@layout/fragment_navigation_drawer"
                tools:layout="@layout/fragment_navigation_drawer" >
            </fragment>

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

Reference: http://hmkcode.com/material-design-app-android-design-support-library-appcompat/