How to change background when changing screen orientation on Android

Asked

Viewed 809 times

-2

I’m developing an Android application where the background image is only cool when the phone is upright. I would like every time the user changes the screen orientation the background changes to an image that looks cool without getting all stretched out.

2 answers

1

You could use a vector image, but if you don’t want to just do the following:

1º => change the view of your project from "android" to "project".

inserir a descrição da imagem aqui

2º => Then create in the folder "res" the folders drawable-land and drawable-port

inserir a descrição da imagem aqui

Add your background to vertical in the drawable-port folder and the horizontal in the drawable-land folder.

OBS => WITH THE SAME NAME!!!

3º => Both your horizontal and vertical background must have the same name, for in the . xml of Activity you do the following:

android:background="@drawable/background"

When the device is upright, the image contained in the drawable-port will be displayed and when it is moved to the horizontal the image contained in the drawable-land will appear.

About making 2 backgrounds, one vertical and one horizontal take the following as example:

horizontal (1280x720):

inserir a descrição da imagem aqui

vertical (720x1280):

inserir a descrição da imagem aqui

-1

Just make a media thing for him. Type:

// Horizontal orientation

@media all and (orientation: portrait) { 

background-image: URL(image-horizontal.PNG) }

// Upright

@media all and (orientation: Landscape) { background-image: URL(image-vertical.PNG) }

From what I understand :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.