I am building a current weather app using the openweathermap API in Kotlin and would like the background to change depending on the state of the weather, e.g. in winter when it snows, the background is snowy (from my drawable folder) and so on.
Below is a function that should change the background depending on the id. But I need to make it automatic.
private fun updateUI(id: Int){
if(id in 200..232){
binding.ivWeatherBg.setImageResource(R.drawable.thunderstorm_bg)
binding.ivWeatherIcon.setImageResource(R.drawable.thunderstorm)
} else if (id in 300..321){
binding.ivWeatherBg.setImageResource(R.drawable.drizzle_bg)
binding.ivWeatherIcon.setImageResource(R.drawable.drizzle)
} else if (id in 500..531){
binding.ivWeatherBg.setImageResource(R.drawable.drizzle_bg)
binding.ivWeatherIcon.setImageResource(R.drawable.rain)
} else if (id in 600..620){
binding.ivWeatherBg.setImageResource(R.drawable.snow_bg)
binding.ivWeatherIcon.setImageResource(R.drawable.snow)
} else if (id in 701..781){
binding.ivWeatherBg.setImageResource(R.drawable.mist_bg)
binding.ivWeatherIcon.setImageResource(R.drawable.mist)
} else if (id == 800){
binding.ivWeatherBg.setImageResource(R.drawable.clear_bg)
binding.ivWeatherIcon.setImageResource(R.drawable.clear)
} else {
binding.ivWeatherBg.setImageResource(R.drawable.clouds_bg)
binding.ivWeatherIcon.setImageResource(R.drawable.clouds)
}
}
I seem to have prepared everything, but when I add the background change function, it all depends on the weather id that I need to write, and I want to automate all this according to the weather.