Keker
Keker

Reputation: 176

How do I have the same toolbar for every layout with Android?

I'm currently trying to create an interface for an application on Android. I'm a beginner with Android, and I would like the same toolbar on every layout I create. Here's my code :

activity_main.xml :

<?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"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    android:background="#FFC8A6"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="#FF0000" app:popupTheme="@style/AppTheme.PopupOverlay">
        </android.support.v7.widget.Toolbar>

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

    <include layout="@layout/content_main"/>

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

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <Button
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:layout_weight="50"
        android:text="Start training"
        android:id="@+id/buttonStart"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp" />

    <Button
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:layout_weight="50"
        android:text="Help"
        android:id="@+id/buttonHelp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="160dp" />
</RelativeLayout>

Now, I'd like to keep what's in activity_main.xml and apply it to every layout, so just the content would change. I know that the color in content_main.xml will have to be applied everywhere, but is there a way to at least keep the toolbar? Thank you.

Upvotes: 2

Views: 503

Answers (1)

Tonespy
Tonespy

Reputation: 3387

Create a layout E.g: 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:title="@string/app_name"
android:titleTextColor="@color/white"
android:theme="@style/ToolbarStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="@color/form_completion_top_text"/>

And in every layout, just place this line of code anywhere

<include @layout/toolbar/>

By default, you'll have the same toolbar

Upvotes: 1

Related Questions