Use Toolbar across all activities (Android)
Asked Answered
D

2

32

I'm using a Toolbar to replace the ActionBar. All is going well with one problem:

The toolbar shows only on the main activity.

If I try call the toolbar on any activity the same way I did with the main activity the app will crash when I call that activity.

If I try to inflate the toolbar onCreateOptionsMenu, that activity will crash when I call it.

How can I call/use the same toolbar across all my activities and not just the main one.

here is some pieces of the code:

public android.support.v7.widget.Toolbar toolbar;

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);
    toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.app_bar_id);
    setSupportActionBar(toolbar);
}

The code above works to call the toolbar successfully, but it only works if i use it on the main activity, the rest of the activities will crash if I called the toolbar there the same method shown above.

Some help please?

Thank you.

Edited:

Upon Request here are more code fragments:

app_bar.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:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/actionbarbgcolor"
app:popupTheme="@style/popUpTheme">

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

themes.xml (styles.xml replacement):

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">

<style name="DefaultActionBarTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:textColorPrimary">@color/windowbackgroundcolor</item>
    <item name="android:windowBackground">@color/windowbackgroundcolor</item>
</style>

<style name="popUpTheme">
    <item name="android:textColor">@color/actionbarbgcolor</item>
</style>
</resources>
Deroo answered 22/2, 2015 at 19:5 Comment(4)
"the app will crash when I call that activity" -- use LogCat to examine the Java stack trace associated with your crash: #23353673Swathe
If you want the Toolbar to be persistant, you should instead use a single activity with multiple fragments.Skipton
Did you put the toolbar in all layouts? Post your logcat.Conceptualize
Yes I did all the require steps you've mentioned hence the toolbar worked for the main activity. Check the updated code fragments.Deroo
D
48

I found the solution, I forgot to include the toolbar in the rest of the activities layout files. So I was calling a toolbar that didn't exist in that activity's layout.

I only had it included in the main activity so that's why it worked there and crashed at the rest.

For beginners, this mean the following code must exist in every layout xml file you wish the toolbar to work in:

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

Note that "app_bar" is just the name I called my toolbar with, yours can be different.

Good luck.

Deroo answered 24/2, 2015 at 15:49 Comment(2)
What layout did you wrap the toobar in app_bar.xml?Accusatorial
how about the code fortoolbar events, that needs to be copied and pasted on all activites as well?Wexler
F
3

you can create a layout with follow code that contain toolbar

<?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="wrap_content"
    android:fitsSystemWindows="true"
    tools:context="com.tejariapp.myapplicationtester1.MainActivity">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

pay attention to height value ,if you don't set it wrap_content value you can see that other elements gone below toolbar and then in each layout that you want to have toolbar ,simply you can write this

<include layout="@layout/toolabar" />
Farahfarand answered 20/1, 2017 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.