FaisalAhmed FaisalAhmed - 4 months ago 20
Android Question

How to use Custom Toolbar in entire app

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>

Answer

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