2

I have made Toolbar using coordinate toolbar in my app so that i can hide toolbar while scrolling . I also have ViewPager inside it . So when user scroll only Toolbar hides and tabs stick to the top . But in other activity i just want to display toolbar what is the better way to that ?

I am sharing my coordinator layout below

<?xml version="1.0" encoding="utf-8"?>

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

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


    <android.support.v7.widget.Toolbar                 
xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/orange"
        app:layout_scrollFlags="scroll|enterAlways"
        app:titleTextColor="@color/white"> 
</android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/MyTabLayout"
        style="@style/TabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        app:tabTextAppearance="@style/AppTabTextAppearance">



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

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




    <android.support.v4.view.ViewPager
        android:id="@+id/MyViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/MyTabLayout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        ></android.support.v4.view.ViewPager>



</android.support.design.widget.CoordinatorLayout>
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
FaisalAhmed
  • 3,469
  • 7
  • 46
  • 76
  • Possible duplicate of [Android Layout: Is reusable component UI possible?](http://stackoverflow.com/questions/2289730/android-layout-is-reusable-component-ui-possible) – Mike M. Jul 30 '16 at 05:44

2 Answers2

4

Create a separate layout for toolbar only.

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:background="@color/white"
    app:layout_scrollFlags="scroll|enterAlways"
    app:titleTextColor="@color/white">


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

Then now its in anywhere in your layout.

<?xml version="1.0" encoding="utf-8"?>

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

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


        <include
            layout="@layout/toolbar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <android.support.design.widget.TabLayout
            android:id="@+id/MyTabLayout"
            style="@style/TabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar"
            app:tabTextAppearance="@style/AppTabTextAppearance">


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

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


    <android.support.v4.view.ViewPager
        android:id="@+id/MyViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/MyTabLayout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v4.view.ViewPager>


</android.support.design.widget.CoordinatorLayout>
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

If you just want to display Toolbar inside other Activities, you can add the Toolbar to any root layout of the respective Activity like LinearLayout or RelativeLayout (which ever suits your app's needs).

Varun Kumar
  • 1,241
  • 1
  • 9
  • 18