Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redefine the toolbar style for all my activities globally?

I have a AppCompat21 Appbar/Toolbar in almost all of my Activities. All activities layout files have the following in common:

<!-- material design actionbar & toolbar -->
<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="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        />

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

so instead of having to add these attributes all the time:

 android:theme="@style/AppTheme.AppBarOverlay"
 android:theme="@style/AppTheme.AppBarOverlay"
 app:popupTheme="@style/AppTheme.PopupOverlay"

I'm trying to find out how to redefine the appbar/toolbar look and feel in a global fashion within my app so I don't need this piece of redundant code everywhere.

Is this even possible? I have done it for the button style, but I can't get my head around this one...

like image 727
Pepster Avatar asked Nov 21 '25 11:11

Pepster


1 Answers

You can create a layout resource file containing your toolbar, named toolbar,

<?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"
   app:popupTheme="@style/AppTheme.PopupOverlay"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:theme="@style/AppTheme.AppBarOverlay"
   app:popupTheme="@style/AppTheme.PopupOverlay"
/>

and in your activities you can just simply do

<include layout="@layout/toolbar" android:id="@+id/toolbar">
like image 105
Spirrow Avatar answered Nov 23 '25 22:11

Spirrow