Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Day/Night theme for android app

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:

  1. 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

  2. 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 ?

like image 701
Alin Avatar asked Aug 01 '13 18:08

Alin


People also ask

What is DayNight theme?

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.

How do I add dark themes to my apps?

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.

How do I change the theme on my Android apps?

In the Project pane select Android, go to app > res > values > themes > themes.


2 Answers

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.

like image 161
CommonsWare Avatar answered Oct 04 '22 03:10

CommonsWare


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);

enter image description here

like image 43
nirav kalola Avatar answered Oct 04 '22 02:10

nirav kalola