How to change current Theme at runtime in Android
I would like to see the method too, where you set once for all your activities. But as far I know you have to set in each activity before showing any views.
For reference check this:
http://www.anddev.org/applying_a_theme_to_your_application-t817.html
Edit (copied from that forum):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Call setTheme before creation of any(!) View.
setTheme(android.R.style.Theme_Dark);
// ...
setContentView(R.layout.main);
}
Edit
If you call setTheme
after super.onCreate(savedInstanceState);
your activity recreated but if you call setTheme
before super.onCreate(savedInstanceState);
your theme will set and activity
does not recreate anymore
protected void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme_Dark);
super.onCreate(savedInstanceState);
// ...
setContentView(R.layout.main);
}
Change Activity's theme programmatically
As docs say you have to call setTheme
before any view output. It seems that super.onCreate()
takes part in view
processing.
So, to switch between themes dynamically you simply need to call setTheme
before super.onCreate
like this:
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
Android - How to switch theme on runtime
As you can see theme is setting on the onCreate()
and before setContentView()
. So you should call the oncreate()
method again when you want to change the theme. But the onCreate()
function will be called only once in the life cycle of an Activity.
There is a simple way to do this. I am not sure that it is the best way.
Suppose you want to apply new theme to Activity 1 on a button click.
inside the onClick event
- Save the theme to be applied such that it should be retained even after the application restart (preferences or static volatile variables can be used).
- Finish the current activity (Activity 1) and call a new activity (Activity 2).
Now in Activity 2
- Call Activity 1 and finish current activity (Activity 2).
In Activity 1
- Apply the saved theme inside
onCreate
.
Hope it is not confusing.. :)
Change AppTheme dynamically
I solved it by changing styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/indigoColorPrimary</item>
<item name="colorPrimaryDark">@color/indigoColorPrimaryDark</item>
<item name="colorAccent">@color/indigoColorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.FullScreen">
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="AppTheme.MainActivity">
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
<style name="AppTheme.SearchActivity">
<item name="android:statusBarColor">@android:color/darker_gray</item>
<item name="android:windowAnimationStyle">@null</item>
</style>
<!-- Colors -->
<style name="Indigo">
<item name="colorPrimary">@color/indigoColorPrimary</item>
<item name="colorPrimaryDark">@color/indigoColorPrimaryDark</item>
<item name="colorAccent">@color/indigoColorAccent</item>
</style>
<style name="Blue">
<item name="colorPrimary">@color/blueColorPrimary</item>
<item name="colorPrimaryDark">@color/blueColorPrimaryDark</item>
<item name="colorAccent">@color/blueColorAccent</item>
</style>
<style name="Red">
<item name="colorPrimary">@color/redColorPrimary</item>
<item name="colorPrimaryDark">@color/redColorPrimaryDark</item>
<item name="colorAccent">@color/redColorAccent</item>
</style>
</resources>
And in my BaseActivity i use this code:
getTheme().applyStyle(R.style.Blue, true);
Change android theme at runtime
The theme you are trying to set has its own Toolbar
. For you to get this exception it means that in your layout you already specify a Toolbar
view and you are setting it somewhere in your activity. If you do that, make sure to extend one of the .NoActionBar
themes i.e. <style name="AppThemeRed" parent="Theme.AppCompat.NoActionBar">
.
You might face a new problem though then as there is no "Light background - Dark No toolbar" theme in the AppCompat so you would have to style the toolbar yourself.
You can see an example how to switch themes on Runtime and also see how to style the toolbar in case you have more issues here:
Sample project
Theme declaration
Related Topics
How to Support Arabic Text in Android
Do Fragments Really Need an Empty Constructor
How to Increase Heap Size of an Android Application
Different Resolution Support Android
Android: Expand/Collapse Animation
How to Implement My Very Own Uri Scheme on Android
How to Use the New Sd Card Access API Presented For Android 5.0 (Lollipop)
How to Send Image Via Mms in Android
Failed to Resolve: Com.Android.Support:Appcompat-V7:26.0.0
How to Load an Imageview by Url in Android
Handler VS Asynctask VS Thread
Google-Services.Json For Different Productflavors
Multiple Dex Files Define Landroid/Support/V4/Accessibilityservice/Accessibilityserviceinfocompat