26

This is my toolbar 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:id="@+id/toolbar"
    android:layout_width="match_parent"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp"
    android:layout_height="@dimen/toolbar_height"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:background="@color/primary_color">



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

I want to change app:theme programmatically. How do i do this?

Shehan Ekanayake
  • 1,581
  • 4
  • 20
  • 35
  • You can't change the style and theme. What do you want to do? Maybe there is a different solution. – krystian71115 Jul 04 '15 at 21:27
  • Well i want to let users to choose color of toolbar.... according to the color i want to change the theme.. so if they choose a dark color theme would be ThemeOverlay.AppCompat.Dark.ActionBar otherwise it would be light theme – Shehan Ekanayake Jul 04 '15 at 21:34
  • And link to answer how to set tile color: http://stackoverflow.com/a/26594674/2536878 – krystian71115 Jul 04 '15 at 21:41

2 Answers2

25

You can do this programmatically or with style:

Toolbar toolbar; // your toolbar
toolbar.setBackgroundColor(newColor); // i don't tested this method. Write if it's not working
toolbar.setTitleTextColor(titleColor); // if toolbar is white set title to black, if toolbar is black set title to white

Or you can do it with style:

Add attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="toolbarStyle" format="reference"/>
</resources>

And now change toolbar.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:id="@+id/toolbar"
    android:layout_width="match_parent"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp"
    android:layout_height="@dimen/toolbar_height"
    app:theme="?attr/toolbarStyle"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:background="@color/primary_color">



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

And in styles.xml (if you don't have this create it):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyStyle.Dark" parent="AppCompat.Theme">
        <item name="toolbarStyle">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
    </style>
    <style name="MyStyle.Light" parent="AppCompat.Theme.Light">
        <item name="toolbarStyle">@style/ThemeOverlay.AppCompat.Light.ActionBar</item>
    </style>
</resources>

If you select second method (with styles) you must restart activity and use setTheme method before super.onCreate()

I hope I helped you.

krystian71115
  • 1,927
  • 17
  • 36
  • 1
    Hi, Krystian. The first one way doesn't work. But maybe you now the way how to change the toolbar between fragments replacing inside one Activity instance ? Need to avoid the Activity recreating. Thanks – Taras Vovkovych Jun 23 '17 at 11:13
25

Use the following code snippet to add a theme:

Toolbar toolbar;
toolbar.getContext().setTheme(R.style.ThemeOverlay_AppCompat_Dark_ActionBar);
Shiva127
  • 2,413
  • 1
  • 23
  • 27
John Ruban Singh
  • 1,284
  • 14
  • 19