0

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

kostyabakay
  • 1,649
  • 2
  • 21
  • 32
  • You should put the ViewPager in your xml layout, see here: http://stackoverflow.com/questions/31698756/remove-line-break-in-tablayout/32547335#32547335 for tabs at the bottom: http://stackoverflow.com/questions/32984706/how-can-i-set-tabs-at-the-bottom-and-also-hide-top-actionbar – Daniel Nugent Jan 12 '16 at 01:30
  • @DanielNugent I have special complicated architecture with fragments from this book and I am trying upgrade this app, but I am not really good know how it works at all, so I can't understand why I have lost my Toolbar, FAB and Navigation Drawer in the second activity. If you have free time can you look to my repository and give me more conrete advice? https://github.com/kostyabak/TeamMaster – kostyabakay Jan 12 '16 at 23:34
  • 1
    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. Take a look at the linked answers for implementation details. – Daniel Nugent Jan 13 '16 at 00:45
  • @DanielNugent you should make ^ an answer so OP can approve it. – tir38 Jan 19 '16 at 18:42

1 Answers1

2

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>
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137