-1

I have to change the style of my toolbar. Currently my toolbar's background colour is dark and the title is light colour. So i need to change it programmatically, the background and text colour.

Jerin Babu
  • 11
  • 1
  • 3
  • 4
    What have you tried ? Post your code . Search before asking question . https://stackoverflow.com/questions/31225189/how-to-change-appcompat-v21-toolbar-theme-programmatically. – ADM May 04 '18 at 05:58
  • 2
    I have to change toolbar theme once the activity is created and the view is displayed. So i don't think this is an exact duplicate of the question you have mentioned. – Jerin Babu May 04 '18 at 06:32
  • 1
    @ADM the method you mentioned in the linked post need the activity to be restarted. this might not be the one developers want, mostly. Consider a Single activity application, and in each fragment we need to change the toolbar theme. That method wont be that suitable. – Jemsheer K D Jun 15 '18 at 12:31

1 Answers1

3

You could try creating two different toolbar xml and styles for light and dark toolbars. you could swap out one toolbar by another having different different styles.

Create Two Styles:

<style name="ToolbarStyle" parent="@style/ThemeOverlay.AppCompat.ActionBar">
    <item name="android:background">@color/white</item>
    <item name="colorControlNormal">@color/text_3</item>
</style>
<style name="ToolbarStyleDark" parent="@style/ThemeOverlay.AppCompat.ActionBar">
    <item name="android:background">@color/colorPrimaryDark</item>
    <item name="colorControlNormal">@color/white</item>
    <item name="android:textColorPrimary">@color/white</item>
</style>

And Create Two Toolbars: toolbar 1:

<?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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:minHeight="?actionBarSize"
    android:textColor="@color/black"
    android:theme="@style/ToolbarStyle"
    app:layout_collapseMode="pin"
    app:layout_scrollFlags="scroll|enterAlways"></android.support.v7.widget.Toolbar>

Toolbar 2 :

<?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_dark"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/black"
    android:minHeight="?actionBarSize"
    android:textColor="@color/white"
    android:theme="@style/ToolbarStyleDark"
    app:layout_collapseMode="pin"
    app:layout_scrollFlags="scroll|enterAlways"
    app:theme="@style/ToolbarStyleDark">

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

And change the toolbar in code when necessary using inflating and adding the toolbar to the layout and removing the original toolbar and setting as the toolbar using setSupportActionbar()

toolbar = (Toolbar) findViewById(R.id.toolbar);
View lytToolbarDark = getLayoutInflater().inflate(R.layout.toolbar_dark, coordinatorLayout);
Toolbar toolbarDark = lytToolbarDark.findViewById(R.id.toolbar_dark);
coordinatorLayout.removeView(toolbar);
setSupportActionBar(toolbarDark);
Jemsheer K D
  • 333
  • 3
  • 11