tpdmsales tpdmsales -4 years ago 124
Android Question

how to change a background image depending on whether it is day or night android

I would like to know how to make a background image change depending on it is day or night in my android app.

independently when the app is opened, just it depends on whether it is night or day.


Answer Source

There are two ways of doing this

  1. Using light sensor available on your handset.

  2. Using phones time.

    Calendar c = Calendar.getInstance();
    int timeOfDay = c.get(Calendar.HOUR_OF_DAY);
    if(timeOfDay >= 0 && timeOfDay < 12){
        Toast.makeText(this, "Good Morning", Toast.LENGTH_SHORT).show();        
    }else if(timeOfDay >= 12 && timeOfDay < 16){
        Toast.makeText(this, "Good Afternoon", Toast.LENGTH_SHORT).show();
    }else if(timeOfDay >= 16 && timeOfDay < 21){
        Toast.makeText(this, "Good Evening", Toast.LENGTH_SHORT).show();
    }else if(timeOfDay >= 21 && timeOfDay < 24){
        Toast.makeText(this, "Good Night", Toast.LENGTH_SHORT).show();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download