I have an app and it has two themes: light and dark. When the user switches 
between the theme, I also need to re-theme the AppBarLayout. By default, 
the AppBarLayout looks like this:

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

As you can see, the theme is set to AppTheme.AppBarOverlay, which is defined as 
just being empty.


However, when the main theme switches (which works fine), I then need to also 
switch the theme being applied to the AppBar. Does anyone know how to do it? I 
tried naming a style like this thinking it would auto change:


<style name="AppTheme.Dark.AppBarOverlay" 
parent="ThemeOverlay.AppCompat.Dark.ActionBar">
 <item name="android:background">@color/colorPrimaryInverse</item>
</style>


But that did not work. I then tried using a programmatic way to set the 
background like this:


AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);
appBarLayout.getContext().setTheme(R.style.AppTheme_Dark_AppBarOverlay);
appBarLayout.invalidate();


But that also didn't work (the title TextView's background did not change). I 
also tried making 2 views and using a ViewStub to swap them, but that also 
failed, and I'm not sure why.


I don't really understand why this is so difficult, if the property is there in 
XML why can't it bet set.

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/af4ef154-060f-4d9e-a72e-0e6750d5ce98%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to