Currently the code checks for the time (between 6 and 18 is day), I am open to suggestion for better ways to check for day/night.
Well, I found this, have no idea if I will be able to implement it, but it's a start.
I am having problems importing com.android.server.twilight.TwilightService.
Unfortunately the TwilightService is not exposed to developers so you won't be able to use it.
To calculate sunrise and sunset timing for any particular day it is necessary to have the co-ordinates for that location.
Currently the application is not requesting location permission. You will need to first take a call whether to integrate this feature at cost of location permission or not ?
If the function needs to be implemented then we can use either of the two approaches
Integarte TwilightCalculator in application and use it to select LIGHT or Dark theme
DayNight theme introduced in appcompat library 23
Note: When no location is provided the sunrise and sunset times are constant as explained in the post
You don't need to get the exact location for this calculation, just let users enter geocoordinates as precisely as they desire.
Who does the nontrivial calculation of the position of the sun?
I think solution suggested by @j12i is ideal one. Since we only need the latitude and longitude(approx entered by user) and date(obtained from the device) to calculate sun rise and sun set timing for that date we can implement the feature without having to request additional location permission.
Too much trouble to get the user to get the location coordinates, what I mean is, what would you tell the user to do? Get the coordinates from where?. Also, which library would do the sunrise sunset calculation?
The Geo Co-ordinates can be found using Google Maps. This might not be the most user friendly method but tech savvy people might have some idea as to to their co-ordinates or can find it quite easily.
As for the sunrise and sunset calculation refer my message from earlier
Integrate TwilightCalculator in application and use it to select LIGHT or Dark theme
TwilightCalculator will do the calculation and you will get sunrise and sunset time in long.
One approach for better UX would be to let user control if he wants to grant the application location permission or enter his co-ordinates manually.