1

I have two background images. I want Image A to display when the screen is horizontal and Image B to display when the screen is vertical. Any easy way of achieving this?

James Fazio
  • 6,370
  • 9
  • 38
  • 47

4 Answers4

2

Do this

Result Values for Oreintations will be:-  
Portrait  == 1  
Landscape == 2    
int i = context.getResources().getConfiguration().orientation;
if (i == Configuration.ORIENTATION_PORTRAIT) {
   yourimg.setBackground(yourAimage);
} else {
   yourImg.setBackground(yourBimage);
}
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
1

you can create two xml layouts here one for landscape and other for portrait. In portrait xml you can add different images for background. By this we can use two different layouts for two orientations for a single activity. see the below picture, hope it should help.

image

Community
  • 1
  • 1
android
  • 666
  • 2
  • 7
  • 15
  • I want to keep my layout's the same, and just change the background. – James Fazio Aug 17 '12 at 05:42
  • create the same layout in both layout and layout-land folder with the different Background images.. Now u will get ur expected result.. – mainu Aug 17 '12 at 05:44
  • @James Fazio you have to create two xml files with same name, one under layout landscape and other under layout-portrait. – android Aug 17 '12 at 05:52
1

take two images A & B. save image A in drawable-land and image B in drawable-port

note that the images are saved with same names..

you can check this in graphic layout also..

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
0

Create each XML for horizontal & Vertical

Create one more folder name "layout-land" n just coy & paste the main.xml(your layout file) and just change the background respectavely.

you'll done...

SilentKiller
  • 6,944
  • 6
  • 40
  • 75