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..
Related
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 :)
I want to change the theme of my app with the preference of the user.
I've searched for it already but all tutorials and answers we're about changing the theme statically (e.g setTheme(R.style.Light)).
But i want the theme to be applied dynamically (eg. User chooses Theme "Dark" in the Settings -> Theme "Dark" is applied in the activity).
I'm sorry that i haven't posted some code, but i haven't achieved any good solution for now, i will post some code when i get some.
Can anybody post me some code or thoughts about how to achieve this?
I'm sorry for this elementary question, but this really freaks me out.
Thank you.
I need to make an Activity that will replace the PreferenceActivity. The reason is that we need to save the settings in the SQL. I need a way to style my settings screen to look exactly like the PreferencesActivity. Can someone give me a link to the code of the PreferenceActivity layout code on Android AOSP? or maybe how can I achieve that?
I have been tired apply a style or a layout to a preference, but it didn't work well.
Could anyone help me with this?
I also find someone say it could be done in listview,But I wanna how to make those blank spaces between two block?
Please, help me with this , it has been puzzled me a long time.Thanks!
You can have a transparent divider between ListItems
android:dividerHeight="10 dip"
You can group your preferences by using PreferenceCategories and PreferenceScreens. If you want a preference to have an icon associated with it, you can use the android:icon attribute on the preference. Are there any specific characteristics of the iPhone settings that you're trying to replicate. You're not going to get your preferences to look exactly like the iPhones setting page, but you can certainly mimic certain characteristics.
i want to add themes to my android application. In the application, user will have options to changes theme of the application. Just like OIShopping List appplication. Please help how do i implement this kind of structure to my application.
Any help, tutorials, guidance are highly appreciated.
thanks..
You can use the method
setTheme() within an onClickListener, and inbetween the parenthesis put the theme.