How to set Weather icons for Night and Day both on Android

1.7k Views Asked by At

I am making a weather app. Whole app is done and everything is working fine except NIGHT ICONS. I have set icons for the day. But I don't how to set two different icons on one condition that checks the time, if its day it shows SUN & if its night it should show MOON

Here is my code:

private static String updateWeatherIcon(int condition) {

    if (condition >= 0 && condition < 300) {
        return "tstorm1";
    } else if (condition >= 300 && condition < 500) {
        return "light_rain";
    } else if (condition >= 500 && condition < 600) {
        return "shower3";
    } else if (condition >= 600 && condition <= 700) {
        return "snow4";
    } else if (condition >= 701 && condition <= 771) {
        return "fog";
    } else if (condition >= 772 && condition < 800) {
        return "tstorm3";
    } else if (condition == 800) {
        return "sunny";
    } else if (condition >= 801 && condition <= 804) {
        return "cloudy2";
    } else if (condition >= 900 && condition <= 902) {
        return "tstorm3";
    } else if (condition == 903) {
        return "snow5";
    } else if (condition == 904) {
        return "sunny";
    } else if (condition >= 905 && condition <= 1000) {
        return "tstorm3";
    }

    return "dunno";
}

please help me out.

3

There are 3 best solutions below

2
Suriya On

1) The weather API would never return "sunny" on the night time. So, you may not need to worry about it.

2) OpenWeather API returns the icon code in the response - "icon":"04n" They have a set of weather icons listed in https://openweathermap.org/weather-conditions which you can use or mimic. They do have different icons for day and night.

Hope that helps!

0
Murtaza On

Guys i have found my Solution Thank you all

 long currentTime = new Date().getTime();
            if(currentTime>=sunrise && currentTime<sunset) {
                return "sunny";
            } else {
                return "nighticon";
            }     
0
shaik On
  1. The openweathermap it self allow the trick for setting night and day icons. please refer ti that link https://openweathermap.org/weather-conditions

  2. In the response we get iconId by that we can separate that icons....

hope it will help you