1

I'm trying to put a background image for my app. I used two images for landscape view and portrait view.

I went through this question answers and it didn't help.

Here's my portrait view background.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" > 

Here's my landscape view background.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background_land"> 

When I load my app in the portrait view, portrait background view displays and when I change the orientation to landscape the same portrait image stretches and displays. but, when I load the app in landscape view, landscape background view displays.

What should I do to change the background image when we change the orientation.

  • Edit -

Activity from manifest.

 <activity android:name="com.NICT.nict.LoginActivity" android:configChanges="orientation|screenSize" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

2 Answers 2

2

You could point to the same resource in your layouts and place 2 resources in drawable folder.

1- Layouts

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" > 

2- Drawables

../res/drawable/background (Portrait default mode) ../res/drawable-land/background (Landscape mode) 

Hope it works.

Sign up to request clarification or add additional context in comments.

9 Comments

I have different layouts called, layout, layout-land, layout-large, layout-large-land, layout-small, layout-small-land, layout-sw600dp, layout-sw600dp-land, layout-sw720dp and layout-sw720dp-land. Should I create land drawable s to each drawable and put the landscape image to land drawable.
No matter how many layouts you have. If you create 2 resources in that folders, drawable and drawable-land, the background image changes automatically when orientation changes.
should I create only one drawable-land or create different drawables named drawable-hdpi-land, drawable-ldpi-land, drawable-mdpi-land, drawable-sw600dp-land, drawable-sw720dp-land and drawable-xhdpi-land to different screen sizes
It depends on your needs, if you want the same drawable for each land screen sizes, you must use only a drawable in drawable-land.
Okay. I'll proceed with your answer. Thanks
|
0

In order to overcome this problem,

  1. Create a drawable-land inside res

  2. Copy the landscape background image into that folder

  3. Remove android:configChanges="orientation|screenSize" from manifest

Now, background image loading perfectly without any errors.

Comments