How to enable MODE_NIGHT_YES in every activity with setLocalNightMode()? - android

I recently wanted to create a setting, to enable a night mode in my android app, i searched in the internet and found a nice solution with AppCompatDelegate´s DayNight theme and a short code fragment... :
if(settingsSharedPreferences.getBoolean(getString(R.string.design_dark_design_key),false))
{
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
...to enable dark mode for my MainActivity, if it is enabled in my app settings. It worked perfectly and changed my MainActivity in "DarkMode"! But if i open another activity, this not appears in dark mode like main activity, but if i add the code lines above to this new activity it also starts with dark mode. So my question:
Do i have to call this code lines in every activity or is there a way to set global night mode for any activity in my app?
UPDATE:
I just called the code baove in a new activity before the super.onCreate() and setContentView() methods and my whole app theme changed. But if i call it in MainActivity just the Theme of my MainActivity changes... Its really strange.
Can i change whole theme also in MainActivity?
Thank you in advanced!

Ok, I've found a solution!
For anyone who has the same problem, this is the correct way to apply dark design for your whole application:
I got it from this NICE site
https://blog.iamsuleiman.com/daynight-theme-android-tutorial-example/
It's pretty easy, you just have to use:
AppCompatDelegate.setDefaultNightMode(mode);
instead of
getDelegate().setLocalNightMode(mode);
Quite easy, but really useful!!! Happy coding.
Sorry for my English, I'm German :)

Related

change to dark mode in settingsFragment in kotlin

I would like to do a small upgrade of my application and I would like the theme to change to dark mode after pressing SwitchPreference. I need a detailed guide or someone to explain to me exactly how to do it. If I am just learning to do something in SettingsFragment but it does not come easily to me, please help. If you do not understand the question, please write in the comments, I will try to explain everything so that the help is the best
I don't think this is the easiest way but you can give it a shot.
What you will do is to save a shared preference I will call it theme. And set it to light or dark based on user preference.
Then in homepage on create method call the shared preference and set your app theme based on what your user have set already.
I don't know if you get the idea but this is best I can explain..

Android dynamically set theme not working

In My application :
setTheme(R.style.sometheme)
does not work same when theme is set in manifest, for activity tag as below:
android:theme="#style/sometheme"
I need to set the theme dynamically, in the android application onCreate . I can't do this, since the behavior of both the implementations is different.
Any help would be greatly appreciated.
Got it..
'setTheme(R.style.sometheme)' takes some time to apply, so if you put certain delay, you could see it. Where as if you apply the theme in manifest, it is applied instantly.

Android: Support Fragments don't apply activity theme?

At the moment, I'm stuck with a very annoying kind of "bug" I assume regarding all Pre-Lollipop Android versions. It appears that (support) fragments don't apply the activity's theme they're assigned to. To make my explanations a bit easier, have the following demonstration:
My app runs with a turquoise theme at first. Let's say, the user decided to change the turquoise theme to a red theme. He or she restarts the app and is greeted with the following:
(screenshot taken on an Android 4.4.2 tablet)
Terrible sight, isn't it? However, if I run the same app in an emulator with Android L the whole theme problem doesn't even seem to exist.
There's especially one thing which seems odd about the tablet screenshot. The fragment itself doesn't apply the theme but child components inside the fragment which get added lateron (like the view with the exclamation mark which is hosted by a ViewPager) take and apply the theme as if nothing happened.
I'm not quite sure what the issue might be. I've done everything as stated in every document available. I set the theme before I call setContentView(resource) in the corresponding activity. I tried to do the trick with ContextThemeWrapper but it was no use.
Relevant code:
The activity's onCreate()
The fragment's onCreateView()
The fragment's layout
Attributes
Themes (a lot of them)
I tried to resolve this issue for days now and I still can't find out why this is not working. I haven't found a suitable answer yet and would love some advise.
I've been the victim to my own stupidity. I had another close look on my project setup and found this:
My tablet takes its layout resources from the sw600dp-folder, but I forgot to change the corresponding layout to take attributes instead of hardcoded colors. I think I have to retire after making such a stupid mistake.

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)

Tips for making the Android launcher transparent

I'm designing a custom Android 4.0 (ICS) device for special purpose.
One of the things I'm looking for is a way to make the Android Launcher, when other activities were previously running, show the last running activity as a transparent, dimmed background, with the launcher icons and widgets on top.
What I'm looking for, is something like this: How do I create a transparent Activity on Android?, except as a modification to Launcher2. I guess I can apply a similar style to the launcher as proposed in that SO, but what about the wallpaper?
AFAIU, the wallpaper is rendered by a separate service, and I still want it rendered if there are no other activities behind the launcher.
Any tips?
I figured this one out myself eventually. It turned out pretty simple.
Just modify the theme as suggested in the mentioned SO-article, and simply change inheritance from the Wallpaper-enabled theme.
Also, there were a small code-snippet that was reactivating the wallpaper. Switch that out, and I've now got a transparent launcher, that shows what's going on behind it.

Categories

Resources