I am developing a weather app where I wish to change my activity background as the weather changes like night to day or day to night.I seemed unable to find answers. A little help would be deeply appreciated.
            Asked
            
        
        
            Active
            
        
            Viewed 880 times
        
    1
            
            
        - 
                    possible duplicate http://stackoverflow.com/questions/2748830/how-to-change-background-color-in-android-app – manolodewiner Mar 31 '16 at 17:31
- 
                    draw everything in your custom `Drawable` class – pskink Mar 31 '16 at 17:38
- 
                    pskink,could you elaborate that ? – hemen Mar 31 '16 at 17:38
- 
                    make a custom `Drawable` class and use it in any View by callimg `View#setBackground` – pskink Mar 31 '16 at 17:44
- 
                    psink,sorry to bother you again,I am new here and could you give me a link for guide? – hemen Mar 31 '16 at 17:55
2 Answers
1
            Try this:
final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable( getResources().getDrawable(R.drawable.yourimg));
} else {
layout.setBackground(R.drawable.yourimg);
}
 
    
    
        ashxvi
        
- 144
- 1
- 10
- 
                    Did you test `layout.setBackground(R.drawable.yourimg);`? setBackground expect **Drawable**, not **int** parameter – Gueorgui Obregon Mar 31 '16 at 18:55
- 
                    
- 
                    Please if you find our help valuable, mark the question as solved ;) – Gueorgui Obregon Apr 01 '16 at 04:52
- 
                    
- 
                    
1
            
            
        Add this in your Activity onCreate():
protected void onCreate(Bundle savedInstanceState) {
    //...
    Calendar cal = Calendar.getInstance();
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    boolean isNight = hour < 6 || hour > 18;
    int currentDrawable = isNight ? R.drawable.night : R.drawable.day;
    View decorView = getWindow().getDecorView();
    Drawable drawable = ContextCompat.getDrawable(this, currentDrawable);
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
        decorView.setBackgroundDrawable(drawable);
    else
        decorView.setBackground(drawable);
}      
Hope this helps!!
 
    
    
        Gueorgui Obregon
        
- 5,077
- 3
- 33
- 57
