2

In my application, I have an image as background. When the application is in portrait mode, that's fine. But when it is in landscape mode, the image turns and is stretched.

I know that we can use the layout-land folder, but with this solution, all my layouts will be in double just for an image (not very logical..).

Is there another solution?

PS : My image is just a carbon texture so, a solution to prevent it to turn with orientation would be ok.

EDIT :

hotveryspicy, your solution works BUT, there are activities which have the property android:configChanges="orientation" (in the Manifest), and it doesn't work for them. How to solve this?

In fact, I use this property to save state when the orientation changes but it's not the good way to do that. I need to use onSaveInstanceState for that.

In the meantime, is there a solution which allows me to keep this bad solution?

HerrM
  • 489
  • 2
  • 6
  • 13

3 Answers3

2

ya, first check orientation in onCreate()

Display mDisplay= activity.getWindowManager().getDefaultDisplay();
int width= mDisplay.getWidth();
int height= mDisplay.getHeight();

if(width>height)
{
//Landscape
//set landscape compatible image 
}
else
{
//Portrait
//set portrait compatible image
}
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
1

Would it do the trick if you placed alternate version of the background image into drawable-land res folder? This way you can still use the same layout files and refer to the @drawable/background in them.

svidnas
  • 581
  • 4
  • 8
0

I doubt it would be possible to do what you had described. The only solution which was possible (layout-land) you had already ruled out. It is the only way to go about it I guess.

Rahul Choudhary
  • 3,789
  • 2
  • 30
  • 30