Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use 2 different background between landscape mode and portrait mode

Tags:

android

I have an android application, I would like to know if I can have 1 layout (1 layout xml file) for both landscape and portrait mode. But I want a different background for each mode? Is that possible? Do I need 2 xml file points to different background image? Or I can achieve what I want using 1 xml file?

Thank you.

like image 803
michael Avatar asked Apr 05 '11 21:04

michael


2 Answers

I have achieved the same by creating drawable-land-hdpi folder and copying my landscape background into it.

I did not want to change my code, I just have one XML layout file and two different background.jpg images in two folders drawable-land-hdpi and drawable-hdpi.

like image 152
theczechsensation Avatar answered Oct 12 '22 02:10

theczechsensation


If you're willing to use code to accomplish this, you might try inserting something like

View view = (View) findViewById(R.id.background_carrying_view);
int orientation = getResources().getConfiguration().orientation;
if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
    view.setBackgroundResource (R.drawable.background_land);
} else {
    view.setBackgroundResource (R.drawable.background_port);
}

into your onCreate() and/or onConfigurationChange().

With this method, you would use the same layout for both landscape and portrait.

like image 35
Cephron Avatar answered Oct 12 '22 02:10

Cephron