Question

I am developing an app in which I need to provide different activity's background image on different orientation. So I've approached in this way:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setLanguage();
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                 Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
                 // set background for landscape
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
               // set background for portrait
    }
}

And

android:configChanges="locale|orientation|screenSize"

and in onCreate() I set the background image for portrait assuming user will launch the app staying in portrait mode.

Everything works fine as when users change their mode, the corresponding background is set and so on.

But if a user starts this app when the phone is in landscape mode, as It is shown the portrait image at the first launch as I assumed before user will launch app in portrait mode.

So how can I solve this problem? In one sentence, what is the best way to set different background image for different orientation ? am I in a right track?

Était-ce utile?

La solution

In onCreate of your activity you can check for the current orientation using the this

Configuration newConfig = getResources().getConfiguration();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                 Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
                 // set background for landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
               // set background for portrait
}

Autres conseils

In one sentence, what is the best way to set different background image for different orientation ?

Step #1: Delete everything you've done, most notably the whole android:configChanges stuff. Ignore the background image for now, and get the rest of your configuration change logic working.

Step #2: Create -land versions of the requisite resource directories, for whatever densities of this image that you have (e.g., res/drawable-land-hdpi/ to match your res/drawable-hdpi/)

Step #3: Move the landscape versions into the -land directories, naming them the same as their portrait equivalents (e.g., res/drawable-hdpi/background.png and res/drawable-land-hdpi/background.png)

Step #4: Just refer to common resource name in your android:background attribute (e.g., @drawable/background)

This way:

  • You stick to better configuration-change behavior, and

  • Android will give you the correct background at the correct time

You can check for orientation in onCreate

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int orientation = display.getRotation();
if (orientation == Surface.ROTATION_0 || orientation == Surface.ROTATION_180)
{
    // Portrait
}
else
{
    // landscape
}

check orientation changes after setContentView method

int orientation = getResources().getConfiguration().orientation;
    if (orientation == 1){
        utility.toast("portrait");
    }else {
        utility.toast("landscape");
   }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top