Change Theme while app is running - android

I am working on an app and I want to have a night mode option. I have already created two themes named HoloLight and HoloDark. I can set these themes in the apps manifest and they both work fine. The problem is I can't find a way to switch between the two with code. Is there a good way to do this?

you can use setTheme method in onCreate() Method like
setTheme(android.R.style.Theme_Black);
This should be above setContentView

You can change theme only before setcontentview.
setTheme(android.R.style.HoloDark); so basically to change theme in app you will have to restart your activity. see this example for that. If you have some data to be saved, save that in instance state by using these functions in activity, onSaveInstanceState(Bundle)
onRestoreInstanceState(Bundle)

Related

How to change Android App theme at runtime like Google Inbox App?

The inbox app features a navigation drawer. Upon clicking on any navigation drawer item a fragment is loaded (most probably) and during this transaction the app theme changes. Changing app theme requires setTheme() method to be called before setContentView(..) in the onCreate() method of the activity. The super fluid UI indicate the use of fragments so how is this achieved without recreating parent activity (otherwise there would have been a lag for sure).
The snooze fragment hase oragne like theme
The inbox fragment has blue like them
You can actually change the theme's style, but only before calling setContentView(#ResId int) method. Something like this perhaps?
getTheme().applyStyle(isDashUser ? R.style.redStatusBar : R.style.blackStatusBar, true);
setContentView(R.layout.my_activity);
If you look closely when changing the page there's a slight graduation between the two colours. This probably suggests they have a system separate from theming to re-colour all the UI elements.
One my apps has a very similar colour change feature, and I just have methods set up to manually re-apply the relevant colours to each UI element. Of course Google probably have some super slick way of doing it that they'll never share with anyone.

How to dynamically change the skin of an activity?

How can I change the theme of an Android app in an automated way?
To set the theme dynamically at runtime, call setTheme() in your activity's onCreate() method, before calling setContentView(). To change the theme, you simply need to restart your activity.
Here is a nice tutorial on how dynamically apply themes.
and this one too.

How can i set theme at run time?

Please Anyone know how to set theme using runtime?
getApplication().setTheme() not set theme?
How can i set theme when user select the button and it will set the theme in my application?
you can use setTheme(..) before calling setContentView(...)and super.oncreate() and it should work fine
I haven't tried this, but here is the documentation for ContextWrapper.setTheme(int):
public void setTheme (int resid)
Since: API Level 1 Set the base theme
for this context. Note that this
should be called before any views are
instantiated in the Context (for
example before calling
setContentView(View) or inflate(int,
ViewGroup)).
Based on this description, the activity needs to be destroyed and recreated (as it would normally on an orientation or other configuration change), then call setTheme() within onCreate().
There is an open Google Android issue for this: it appears to be an open bug that you cannot programmatically change the theme with setTheme(). http://code.google.com/p/android/issues/detail?id=4394

how can we dynamically switch on or off from one Theme to another Theme for whole activity

how can we dynamically switch on or off from one Theme to another Theme for whole application...i have try to find everywhere but cant get success.....so,please provide any source code...
setTheme() does work, you just have to be sure to set the theme before any of the system framework starts building views. I have applications that can switch themes on the fly but you must call setTheme() first in your onCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
// The only way the custom themes apply properly is to assign them before any view
// resources are created.
SettingsActivity.setThemeFromPreferences(this);
super.onCreate(savedInstanceState);
}
Where the setThemeFromPreferences method looks up the setting and calls setTheme with the appropriate style. Also take a look at Ben's answer to this post about restarting an activity that you may want to use to apply your theme changes on the fly after they are changed by the user.
You can try with setTheme, but you might run into monsters.
There is an open Google Android issue for this: it appears to be an open bug that you cannot programmatically change the theme with setTheme().
http://code.google.com/p/android/issues/detail?id=4394

Implement change black/light theme feature in Android app

I want to have a "change theme" feature in my app. If I call setTheme() in onCreate(), there is one problem.
The moment after I launch my app, a plain white background appears for a second (because I have set light theme in the manifest). After that, the complete layout of my Activity is displayed - it is either with white or black background, according to user's theme preference.
Is there any way I can change whether white or black background appears after launch?
Make sure you call setTheme() in onCreate() BEFORE calling setContentView(). Then if you want to dynamically change the theme again later, you should simply restart your activity.
If you are adding a theme to the entire program than you could start by doing:
In your manifest you add to your application tag that you are using a theme.
<application android:theme="#style/mythemename">
Then look at Theme XML to make sure that you have what you need declared in the appropriate places.
If it is just for a particular action you could add the activity tag
<activity android:theme="#android:style/Theme.propertyname">
You can also, if you want your theme to just change the background color, follow the same pattern with either the activity or application tag (what ever one you are using) and set the item name "colorbackground" to what you want.
You can also use Theme XML and remake what you want in your current theme and call that your custom theme using the method above.
I hope this helps and if not please let me know so I might be able to help better in the future.
Another way would be to have a kind of splash screen which will check for a preference variable for example and then decide whether to use the light or the dark theme. This way you could also use XML layouts.
EDIT: Yet another way is to have the all the layout defining stuff in the onCreate() method and then just trigger the onStart() method when ready.

Categories

Resources