8

First I set AppBarLayout theme via xml:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/MyTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/MyTheme.PopupOverlay"
        app:titleTextAppearance="@style/MyTheme.Toolbar.Title" />

where @style/MyTheme.AppBarOverlay contains:

<style name="MyTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="colorPrimary">@drawable/toolbar_background</item>
</style>

But then in some scenarios I want to change it programmatically without change the Theme of the activity (only the Theme of the AppBarLayout).

I have tried these two ways without success:

  1. First:

    ActionBar actionBar = getSupportActionBar();
    actionBar.getThemedContext().setTheme(R.style.MyTheme_AppBarOverlay_2);
    
  2. Second:

    AppBarLayout mAppBar = (AppBarLayout) findViewById(R.id.appbar);
    mAppBar.getContext().setTheme(R.style.MyTheme_AppBarOverlay_2);
    
Omar Hemaia
  • 188
  • 10
android iPhone
  • 860
  • 2
  • 11
  • 22

1 Answers1

0

Changing themes after inflation is not possible with Android views. You'll have to change each property the theme defines individually. You may work around this by using Compose TopAppBar, since Compose widgets react to theme changes.

bompf
  • 1,374
  • 1
  • 18
  • 24