I have an app in which I need to implement day / night theme. Unfortunately there is no simple way of making theming by just using styles, I need to be able to update: layout backgrounds, button selectors, text color, text size, images, icons, animations.
From what I see I have 2 options:
Have different xml layout files for night/day, so something like home_day.xml
/ home_night.xml
. There are around 30 screens in the app, so in the end there will be 60 xml layouts. On activity/fragment onCreate, based on current hour I could setContentView
. This adds some more xml files but avoids adding more code in activities
Have only one layout for day/night and on activity's onCreate
findviewById for each item I want to theme and update his attributes based on current day/night. This could generate a lot of extra code, findviews and apply attributes for many views.
I am aiming for for 2. but I am open to any suggestions from you. So, what would you choose and why ?
The DayNight functionality in AppCompat allows your app to easily switch between a dark ⚫ and light ⚪ theme. This has many benefits for your users, from saving power on OLED displays, to increasing usability for people with reduced-vision, and more.
Turn Dark theme on or off in your phone's settings On your phone, open the Settings app. Tap Display. Turn Dark theme on or off.
In the Project pane select Android, go to app > res > values > themes > themes.
I'd use -night
as a resource set qualifier for night mode, putting your night-specific resources in there.
Android already has the notion of night mode, switching between night and daytime modes based upon time of day and sensors. Hence, you might consider using it.
For example, to have a different theme based on mode, create res/values/styles.xml
and res/values-night/styles.xml
. Have a theme with the same name in each file (e.g., AppTheme
), but tailor the theme based upon whatever differences you want have between day and night modes. When you reference your theme by name (e.g., in the manifest), Android will automatically load in the right resources, and Android will automatically destroy and recreate your activities if the mode changes while those activities are running.
Now, if you want manual user control over whether to use a night-themed UI, -night
will not help.
Check this tutorial for a complete step by step example: click here
Add Auto Switching DayNight Theme using Appcompat v23.2 support library
Add following line in your build.gradle
file
compile 'com.android.support:appcompat-v7:23.2.0'
Make your theme style in styles.xml
as below
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
<item name="android:textColorSecondary">@color/textColorSecondary</item>
</style>
</resources>
Now add the following line code onCreate()
method for setting theme for entire app.
For Setting Default Auto Switching Night Mode use
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
For Setting Default Night Mode use
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
For Setting Default Day Mode use
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With