kostyabakay
kostyabakay

Reputation: 1689

How to make visible Toolbar with ViewPager?

I am reading "The Big Nerd Ranch Guide" on 11 chapter and I am writing very similar application with Material Design. Application has two activities - list and item detalization from this list. In detalization activity was implemented ViewPager in onCreate method with this code:

mViewPager = new ViewPager(this);
mViewPager.setId(R.id.viewPager);
setContentView(mViewPager);

How I understand in setContentView we don't use XML markup, so I have lost my Toolbar.

scr1 scr2

In next chapter of the book application has ActionBar. How can I return my Toolbar in detalization activity?

book scr

Upvotes: 0

Views: 1019

Answers (1)

Daniel Nugent
Daniel Nugent

Reputation: 43322

You lost the TabLayout and the FAB because you're calling setContentView() with only a ViewPager.

If you want to have a ViewPager and a TabLayout and a FloatingActionButton, just call setContentView() on an XML layout that has all three. Something like this will put the TabLayout at the top:

<RelativeLayout
    android:id="@+id/main_layout"
    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"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        app:tabMode="fixed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        app:tabTextColor="#d3d3d3"
        app:tabSelectedTextColor="#ffffff"
        app:tabIndicatorColor="#ff00ff"
        android:minHeight="?attr/actionBarSize"
        />

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

</RelativeLayout>

Upvotes: 2

Related Questions