Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Dark Mode on Android App with Kotlin

 

 
Manuel Ernesto 🇦🇴🔥
Android Developer | kotlin & Firebase 🔥 lover |Lead Organizer @GdgLubango
Published: July 03, 2020
Tweet
Share
 

 

On the Google I/O 2019, the Google team presented the possibility to developers add a dark theme to the android applications and since then many application has already been updated with this feature like example: Youtube, Instagram, Chrome and so many others.

In this article, I will explain how you can implement dark mode in your application in an easy way.

 

The benefits 🌚

As you know, the dark mode can bring to people some benefits like:

  • It can reduce power usage by a significant amount.
  • Improves visibility for users with low vision and those who are sensitive to bright light.
  • It makes it easier for everyone to use a device in a low-light environment.

 

Recommendations 👍🏿

Your themes and styles should avoid hard-coded colors or icons intended for use under a light theme. You should use theme attributes (preferred) or night-qualified resources instead.

You might want to allow users to change the app’s theme while the app is running. Your app can let the user choose between themes considering the following recommendations:

When running on devices running Android 9 or earlier, the recommended theme options are:

  • Light
  • Dark
  • Set by Battery Saver (the recommended default option)

When running on Android 10 (API level 29) and higher, the recommended options are different, to allow the user to override the system default:

  • Light
  • Dark
  • System default (the recommended default option)

Let’s code 👨🏿‍💻

First, go ahead, and create a new Android project.

In the activity_main.xml file, we will have just one Textview to show some text and one Button to allow us to change the theme.

 

 

As you see when running the app

 

light theme

 

After that, we need to change the android style to (in my case)

 

 

Or to the material component:

 

 

Now let’s go to the MainActivity Class and create the method chooseThemeDialog and inside add the code to create an AlertDialog, to allow the user to choose the theme.

 

 

In the onCreate method let’s call the chooseThemeDialog method when the user clicks on the button

 

 

Run the app again and let’s see the result🧐 when we click on the button

 

light theme

 

So, let’s go inside to chooseThemeDialog method and add the code to change the theme and apply to our App using the when condition and AppCompatDelegate.

AppCompatDelegate is a class in support library of android that allows us to work with Dark Theme implementation. for that's work we will com a static method call setDefaultNightMode and pass the mode as a parameter, like:

  • AppCompatDelegate.MODE_NIGHT_NO: means night mode is not active, we’re using the light theme
  • AppCompatDelegate.MODE_NIGHT_YES: means night mode is active and we’re using dark theme
  • AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM: means we will follow the system definition

 

 

Run the app and check the result when we click on dark on the AlertDialog

 

dark theme

 

But we can improve the code, by saving the choice of the user, in this case, we can use SharePreferences for this.

For it works, we need do added the new SharedPreferences dependency to the build.gradle file.

 

 

Let’s create the class MyPreferences to help us manipulate and store the value on the SharedPreferences.

 

 

Now we can update our chooseThemeDialog method to save the choice of the user and keep the theme even when we close the app

 

 

Create also the checkTheme method to check on the onCreate method what theme status we saved on SharedPreferences

 

 

Now as we already said, we need to call the checkTheme method inside the onCreate.

 

 

Now the final result🎉

 

app demo

App Demo

 

So, that is it for now, and don’t forget to see the full code on my Github

 

manuelernesto/darkmode_app_with_kotlin

Contribute to manuelernesto/darkmode_app_with_kotlin development by creating an account on GitHub.

github.com

 

Thanks to you for reading this post! Please do 👏🏿 if you liked it and want more posts about android development.

 

 

Tags: Kotlin, Android, Dark Mode, Android App Development, AndroidDev

 

View original article at: 


 

Originally published: June 02, 2020

Android News
Evolution of Android Update SystemEvolution of Android Update System
Evolution of Android Update SystemEvolution of Android Update System

By Ivan Kuten

So, how can you update Android on mobile devices? While developing software for Smart TVs and Android-based set-top boxes, we’ve narrowed it down to four ways, discarding some very exotic options:

By ProAndroidDev -
Android News
Happy Railway
Happy Railway

By Hadi Lashkari Ghouchani

This post is on the tail of Railway Oriented Programming in Kotlin by Antony Harfield. So you need to read it first and continue here. As it’s obvious I really liked it and tried it out. It needs every process have a result like

By ProAndroidDev -
Android News
Unit Tests and Concurrency
Unit Tests and Concurrency

By Stojan Anastasov

Once Retrofit added RxJava support, RxJava became my go-to concurrency framework for writing Android apps. One of the great things about RxJava is the excellent testing support. It includes TestObserver, TestScheduler, RxJavaPlugins so you can switch your schedulers in tests.

By ProAndroidDev -
Android News
When Compat libraries will not save you
When Compat libraries will not save you

By Danny Preussler

And why you should avoid using the “NewApi” suppression! The idea of “Compat” libraries was probably one of the key aspects of Android dominating the mobile space. Other than with iOS, Android users often could not update their operating system after a new version launch, simply as their phones won’t allow them to, the Android problem of fragmentation.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One