0

I am trying to create an app with different background images when in portrait vs landscape mode on a fullscreen activity, but, it always uses the background image from the portrait layout for both.

Does anybody know why this happens?

closed as off-topic by bmdixon, eldarerathis Aug 3 '15 at 14:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about writing and publishing Android apps, writing custom ROMs, and other topics that are primarily of concern to developers are off-topic. Consider taking advantage of other resources that may be appropriate for your question." – bmdixon, eldarerathis
If this question can be reworded to fit the rules in the help center, please edit the question.

1

After a lot of googling and combining results from different locations, I found an answer that works for me.

First of all, if the AndroidManifest.xml file contains

android:configChanges="orientation"

Then it means that your activity config won't refresh when orientation changes, and you are given the opportunity to handle it manually via onLayoutChange, which I chose to do as follows:

// Use a layout change listener to re-apply the background image if
// orientation changes.

   fScreenView.addOnLayoutChangeListener(new View.OnLayoutChangeListener()
   {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
                                   int oldTop, int oldRight, int oldBottom) {
            int orientation = getResources().getConfiguration().orientation;
            if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
                fScreenView.setBackgroundResource (R.drawable.sab_logo_text_2);
            } else {
                fScreenView.setBackgroundResource (R.drawable.sab_logo_text_1);
            }
        }
    });

If that does not float your boat, you can always just let Android handle it for you by killing and restarting your activity in the correct layout (in my case, correct orientation) by simply removing

android:configChanges="orientation"

from the manifest file. In my case, the actual default value was

android:configChanges="orientation|keyboardHidden|screenSize"

and needs to be changed to

android:configChanges="keyboardHidden|screenSize"

After which the activity will be killed and restarted in the correct layout. On my application, the in-code method proved to be faster, so I handled it in code.

Not the answer you're looking for? Browse other questions tagged or ask your own question.