0

My application has several activities. For all of them, the toolbar is implicitly inherited from the Main activity. I would like to set a specific toolbar only for a single activity, but for all others - I want to continue inheriting them from the Main one.

If I try to set a new toolbar to my single activity - I get a well-known exception This Activity already has an action bar BUT to fix it, I can add to my AppTheme style:

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

which allows me to set different toolbars,

but the problem is: in that case for each activity, I need to explicitly set a toolbar, but I want to inherit a toolbar from the main one.

Please find some code below:

My main 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="?attr/actionBarSize"
                app:popupTheme="@style/AppTheme.PopupOverlay" android:layout_weight="1">
        </android.support.v7.widget.Toolbar>

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

and styles:

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@android:color/white</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Light"/>
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

and the main activity in manifest.xml

        <activity
                android:name=".."
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                ..
            </intent-filter>
        </activity>

and the MainActivity onCreate method:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar); 
        ...
    }   
a3dsfcv
  • 1,146
  • 2
  • 20
  • 35

1 Answers1

1

To share a single toolbar, your best bet is to extract the toolbar layout to a separate layout file, e.g., app_bar.xml, and then to include it in each activity you want to use it in with the following line:

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

Then, in the activity where you need a customized app bar, you just don't use this app bar but rather a newly customized one.

Suppose the app bars share a lot of customization and you don't want to repeat yourself. In that case, you can create a custom theme for your app bar and set this for your toolbar, extend this theme in another theme, overwrite the attributes you want to change, and set the extending theme for the particular use case.

Igor
  • 114
  • 1
  • 12
  • Yes, that's basically the solution that I also discovered, but in this case, you also need to manually set a toolbar for every Activity. But seems there is no other way for this. Thanks for your reply! – a3dsfcv Jan 04 '22 at 15:06
  • Yeah, though you can make your own BaseActivity class which would contain the code for the setting of the toolbar, then make your other activities extend the BaseActivity. Also make sure that your toolbars then have the same id inside the layout files – Igor Jan 04 '22 at 20:15
  • Hey @Igor can you please help me on [this](https://stackoverflow.com/q/72685383/11560810) – Kotlin Learner Jun 20 '22 at 17:37