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);
...
}