4

I have just started developing an android weather app and I was wondering how to change activity background automatically. For example, in daytime it should show day time or in the night it should show night photos.

This is the app of Sony which has a feature (mentioned above)

Check the screenshots.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • 1
    There's no automatic. What's wrong in checking your requirements `if(something)` and changing the background with the normal Java calls`getWindow().setBackgroundDrawable(drawable);` ? – Budius Apr 10 '16 at 12:13
  • night mode in layout. check its tutorial. it may help u. – vabhi vab Apr 10 '16 at 12:16
  • Budius,I tried,but that din't slove the problem –  Apr 10 '16 at 12:32

2 Answers2

2

Okay Credit goes to SteD;so for you check this(beginner's guide) Follow this //set an ID for Relative Layout in content_main.xml(Android Studio)

 RelativeLayout rlayout=(RelativeLayout)findViewById(R.id.rlayout); 
if(something){Drawable drawble=getResource().getDrawable(R.drawable.your_image);rlayout.setBackgroundDrawable(drawable);}

//If it works,destroy the upvote

hemen
  • 1,460
  • 2
  • 16
  • 35
0

The only automatic way is the newly released (Day/Night theme for android app)

For finer control you check the condition yourself and call the normal Java methods, like this:

if(something) {
   getWindow()
      .setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.image));
}

of if you don't care about the newly introduced context themed styling, you just call the deprecated method (which will keep working without issues for all the foreseeable future)

if(something) {
   getWindow()
       .setBackgroundDrawable(
              getResources().getDrawable(R.drawable.image));
}
Community
  • 1
  • 1
Budius
  • 39,391
  • 16
  • 102
  • 144