

Visualize the effect of color changes on common UI elements.Adjust themes for different resource classifiers.If not, you will have to loop through all the View in your layout and check them.Warning: Starting with version 3.3, Theme Editor is no longer included with Android Studio.Īndroid Studio includes a visual assistant called Theme Editor that helps you: If you know which Views are likely to be affected and can check them directly, nothing could be better. In this scenario, you would likely have to check the appropriate Views or all Views to see if they are consistent with your set theme. Recursively change attributes on all your ViewsĪs much as we hope that the theme can contain all our formatting, we invariably need to override a text colour or background colour in-line in our layout or an in a style attribute and this needs to be changed programmatically. The easiest solution to this is to close all the backgrounded Activities, or else, you’ll have to save their state, close them and relaunch them in `onStart` or `onResume`. Any Activities that are already open in the background will not have the theme change applied to it when you go back to them.This depends a lot of how heavy your Activity and it’s layouts are. Saving your Activity state and relaunching it may not be as smooth as in my example above.In order to achieve theme change in this manner, you have to make sure that all your View inherit attributes that matter from the theme and do not in-line any attributes that matter like background colour or text colour.
#Change android studio theme code
The code to achieve this is in my gist " Transition themes". If you don’t want a fade in effect, remove all animations for Activity transition and you should have a sudden change. The result of this approach is shown in the video below.Īs you can see, the approach produces a pretty nice result.

In order to affect change on the current Activity, we’ll have save the state of the current Activity and relaunch the activity, in order to make this experience seamless for the user, you have 2 options, either remove all transition animations for Activities or change them to provide a nice fade in effect. This is pretty straight forward, however this works when an activity is first created and has no effect on the current open Activity or any backgrounded Activities.

SetTheme(darkTheme ? R.style.AppThemeDark : R.style.AppThemeLight) Changing the theme is fairly straight forward, something like below should do it: Changing the theme at runtimeĪs mentioned before, it’s only possible to change the theme of an Activity in the `onCreate` method and that to only before `super` has been called. I've provided an example implementation of each of these methods below. Neither of these options is ideal, you may even want to consider a hybrid of these two approaches. This way, when the theme is changed, you can loop through all the Views again and change the attributes to reflect the new theme. So our second option is to recursively loop through all of our views and set their attributes each time an Activity or Fragment is created. This is problematic because it’s hard to provide a seamless experience to the user if you have to restart the app or Activity in order to change the theme. That being said, it is possible to change the theme of an Activity, however only in the `onCreate` method and only before `super` is called. The use case is often that there is a setting, button or check box which can switch between different colour themes or between something like day and night mode.Įvery time such a requirement comes up, the first thing a quick Google search shows is that it’s not possible to change themes at runtime.
#Change android studio theme how to
Every so often, I see a question posted on StackOverflow which is effectively asks how to change the themes of an app at runtime.
