How to change AppCompat v21 toolbar theme programmatically?
Asked Answered
R

2

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?

Rennet answered 4/7, 2015 at 21:19 Comment(3)
You can't change the style and theme. What do you want to do? Maybe there is a different solution.Incompatible
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 themeRennet
And link to answer how to set tile color: https://mcmap.net/q/362740/-how-to-change-action-bar-title-color-in-codeIncompatible
I
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.

Incompatible answered 4/7, 2015 at 21:54 Comment(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. ThanksKinsler
S
25

Use the following code snippet to add a theme:

Toolbar toolbar;
toolbar.getContext().setTheme(R.style.ThemeOverlay_AppCompat_Dark_ActionBar);
Servais answered 24/3, 2017 at 9:4 Comment(6)
Worked For me.. ThanksCaerphilly
Yes! This worked! All the other answers were both way overkill and some didn't work. The ones I tried didn't work, anyway. Thank you.Christenson
For me this did more than I wanted. It set the theme for my entire activity, not just the toolbar. Be careful if you have other elements in your activity that respond to the theme.Airman
This is dangerous, it might mess up your activity's themeObturate
It didn't work for me (for API 30). It will definitely apply the theme to the toolbar's context which is obviously your activity.Ynes
How would I do this if it were a custom toolbar implementation? I want to conditionally switch styles.Hut

© 2022 - 2024 — McMap. All rights reserved.