Android Switching between Dark/Light themes not working - android

I am working on an Android project where I need to support switching between Dark/Light themes based on system setting preference.
I have changed App Theme from Theme.AppCompat.Light.NoActionBar to Theme.AppCompat.DayNight.NoActionBar.
I am using this API on Application onCreate()
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
App theme is getting updated only when I restart app but does not update when I am within the app when theme is changed from system setting.
Am I missing any config changes?

I believe that the issue lies with the android:configChanges tag. This is because the activity needs to be recreated when the theme is changed. You will have to remove the tag and use onSavedInstanceState to save the state of your current activity.

Related

Android: Must a Fragment's Theme be the Same as the Main Activity/App Manifest?

I made a new Fragment and tried to set it's theme to AppCompat.NoActionBar using Android Studio's Design tool. The problem is, once I selected it, the theme did not change and stayed on the default app theme.
After Manually Setting the theme on the Android Manifest to Theme.AppCompat.NoActionBar the Fragment then accepted the Theme Change.
I want to ask the following:
Can Fragments have different Themes independent from the App Manifest or Main Activity? (Since I assume setting the Theme in the App Manifest changes it for all Activities and Fragments).
I was looking for a way to just set the Fragment's theme to AppCompat.NoActionBar but didn't get a way to do it without setting it in the App Manifest.
This is my First Question on StackOverFlow, hope it's a valid question since I couldn't find the answer online. :)

Day/night theme according to the time

I'm developing an android app where i want to have a night theme also. I don't know how to proceed here. I want an option to be set in settings that automatically set the day/night theme. And theme should be set according to the time same as in google maps.
How can i do this?
You just need to add the timings at which the night theme would be changed. Checkout the this AppCompat v23.2 — DayNight

Change Theme while app is running

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)

android - how to loop through all activities?

In my android app, i can change the theme, but to see the change I have to exit the app and open it again.
This is how I change the theme.
ThemeSetterActivity.setStyle(signup.this);
which happens on the create event.
But is there some code like this (below) that I can run, and will change the theme for all the activities.
foreach (activity act in app) {
ThemeSetterActivity.setStyle(act.context);
}
thanks
As far as I know you can't do it that way.
What I do is have each activity keep track of what theme it is currently using.
Then in each activities on resume just check if the current theme is still what it should be, if not restart the activity.
An easy way to get that application wide is to just have your base activity class implement that behavior, and then have all activities extend that.
Apply a theme to an Activity or application
To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the tag to include the android:theme attribute with the style name. For example:
<application android:theme="#style/CustomTheme">
If you want a theme applied to just one Activity in your application, then add the android:theme attribute to the tag instead.
Just as Android provides other built-in resources, there are many pre-defined themes that you can use, to avoid writing them yourself. For example, you can use the Dialog theme and make your Activity appear like a dialog box:
<activity android:theme="#android:style/Theme.Dialog">
use static field and store your theme id;
and in onCreate method use :
yourActivityObj.setTheme(R.style.AppTheme);
maybe id work;

Choosing a style / theme programmatically at run time

I have device A and device B.
I can easily detect if the app is running on device A or on device B.
Now what I need is to use on theme (styles) for device A and other on device B.
How can I do this?
In your Activity.onCreate(), you can call setTheme() to set the theme you would like to use. Note this must be done before you call setContentView() or otherwise create your UI.
Keep in mind that when the user launches your app, the system will show a preview of it while this happen. This previous is based on creating a window that matches the theme declared in your manifest. You want this to match as closely as possible the themes you are going to set in your onCreate() to make the transition to your app as smooth as possible.
If you want your theme to vary based on some device configuration -- such as platform version or screen size -- you can do this all through resources. Just declare different versions of your theme for the different configurations you want. The file layout would be something like:
values/
styles.xml # Required default theme
values-v11/
styles.xml # Theme when running on Android 3.0 or higher
values-xlarge/
styles.xml # Theme when running on an xlarge screen
The -v11 allows you to have a version of the theme that uses a new theme when running on newer platforms while reverting to something compatible on older versions. For example in the values-v11 style your theme's parent could be the new #android:style/Theme.Holo, while the basic one would inherit from the older #android:style/Theme.
Also Android 3.0 gives you a way to change your theme at runtime, by asking that your activity being restarted like when a configuration change happens: http://developer.android.com/reference/android/app/Activity.html#recreate()
After calling that, the new instance of the Activity that gets created can call setTheme() with a different value (based for example on information in the saved instance state or a shared preference) than the theme that was previously being used.

Categories

Resources