Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CollapsingToolbarLayout and TabLayout

I'm trying to create an activity that has a CollapsingToolbarLayout with an image and toolbar (like in the CheeseDetailActivity in the cheesesquare example here), that also has a tab layout below.

Any ideas how to implement it?

When trying to add it to the CollapsingToolbarLayout or the AppBarLayout, the result is that the tab layout is in the top of the screen

like image 971
Roi Divon Avatar asked Jun 03 '15 16:06

Roi Divon


People also ask

What is TabLayout?

TabLayout is used to implement horizontal tabs. TabLayout is released by Android after the deprecation of ActionBar. TabListener (API level 21). TabLayout is introduced in design support library to implement tabs. Tabs are created using newTab() method of TabLayout class.

Can we use TabLayout without ViewPager in Android?

It is possible to use a TabLayout without a ViewPager by using a TabLayout. OnTabSelectedListener . For navigation within an Activity , manually populate the UI based on the tab selected.

How do you make TabLayout?

This example demonstrates how do I create a Tab Layout in android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 3 − Add the following code to res/layout/activity_main. xml.


1 Answers

Try this structure:

<android.support.design.widget.CoordinatorLayout
    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">

    <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.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="SPECIFIC HEIGHT HERE!"
        android:fitsSystemWindows="true"
        android:theme="ADD A STYLE HERE IF YOU WANT">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            >

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="YOUR SOURCE"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="YOUR MULTIPLIER"
                />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="YOUR POPUP THEME">

            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:tabGravity="YOUR TAB GRAVITY"
                app:tabIndicatorColor="YOUR TAB INDICATOR COLOR"
                app:tabMode="YOUR TAB MODE">

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

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

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

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

The important attribute is the layout_gravity of the TabLayout to be bottom.

For API 21 and lower I encountered the issue of the TabLayout disappearing. If you face the same problem, for the TabLayout (for APIs lower than 21) use this:

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:translationY="YOU HAVE TO EXPERIMENT WITH THIS ATTRIBUTE - (in dps)"
    app:tabGravity="YOUR TAB GRAVITY"
    app:tabIndicatorColor="YOUR TAB INDICATOR COLOR"
    app:tabMode="YOUR TAB MODE"
    >

You have to experiment with the translationY attribute depending on the size you gave your app bar. You will enter a value in dps and in a few minutes you will nail it.

Hope it works for you as it worked for me!

like image 71
TheoK Avatar answered Oct 07 '22 13:10

TheoK