Karl Caday
Karl Caday

Reputation: 83

When the screen rotates, the layout changes

I have a project and when I start it on emulator the layout is in order, but when i rotate the screen, the layout changes. For example when the project starts the textview1 is on top of textview2, but afeter I rotate the screeen, textview1 and textview2 are on the same place. how can i handle this? when the screen rotates. i want the layout to adapt to the device. for example it is installed on a tablet. the layout is still the same. and when it is installed on smartphones and the screen rotates i want that the layout is still the same.

Upvotes: 1

Views: 751

Answers (4)

saurabh
saurabh

Reputation: 219

you should use relative-layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
    android:id="@+id/TextView01"
    android:text="RED"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"></TextView>
<TextView
    android:id="@+id/TextView02"
    android:text="ORANGE"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@+id/TextView01"></TextView>
</RelativeLayout>

and if you want to handle change in configuration add

android:configChanges="keyboardHidden|orientation|screenSize" to your Manifest

Upvotes: 0

Manolis Proimakis
Manolis Proimakis

Reputation: 1785

In general, if you only have one layout folder, your layout should be the same in landscape and portrait mode both on tablets or phones because android can't find another layout to use.

Android system uses specific layout if a condition is met for the device:

e.g if device is in landscape it tries to load the layout with name [some-layout.xml] from folder "layout-land", but if it can't find your [some-layout.xml] file inside that folder or the "layout-land" folder does not even exist, it uses the [some-layout.xml] layout in your default "layout" folder.

So i believe that there must be a problem in your layout file that places the TextViews on the same place in landscape mode.

Otherwise, if you want a different layout in portrait and different in landscape, create a new folder in your resources with the name "layout-land" and add inside a copy of your layout and add how you want your textviews to be placed diffrently in landscape mode.

Upvotes: 1

KDeogharkar
KDeogharkar

Reputation: 10959

Create two folder for your layout : first is layout-land and other is layout-port, create two different xml file with same name and according to landscape or portrait view each forlayout-land and for layout-port design your layout (xml file)
Device will automatically take the xml file as per the orientation.

More info here:

http://developer.android.com/guide/practices/screens_support.

---layout-land(folder name)
   ---yourlayout.xml  // same name file with design according to landscape mode
---layout-port(folder name)
   ---yourlayout.xml  // same name file with design according to portrait mode

Upvotes: 3

Mehmet Emre Portakal
Mehmet Emre Portakal

Reputation: 1784

you have to create layout files for each screen sizes and orientation

res/layout/my_layout.xml             // layout for normal screen size ("default")
res/layout-small/my_layout.xml       // layout for small screen size
res/layout-large/my_layout.xml       // layout for large screen size
res/layout-xlarge/my_layout.xml      // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

http://developer.android.com/guide/practices/screens_support.html

Upvotes: 1

Related Questions