Screen Orientation in Android

                                      Support Screen Orientation in Android



Android project defaultly having main.xml file in layout folder. It supports both portrait and landscape.
But some times our layout is not aligned well when skip to landscape mode.
So we cannot able to get an expected view.Ex: textview ,button are misplaced.
We can overcome this problem by using "layout-land" folder in our application.

Step 1:

Copy the below code and paste it in main.xml file which is in layout folder.

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical" android:gravity="center">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Portrait"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center"/>

  </LinearLayout>

Step 2:
Create a "layout-land" folder in res folder.That is

res ->rightclick ->new ->folder-> name the folder name as "layout-land"

Step 3:
create a main.xml file in "layout-land" folder.Then Copy the below code and paste it in main.xml file which is in layout-land folder.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical" android:gravity="center">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LandScape"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center"/>

</LinearLayout>

Now run your program it will produce a result as

Portrait mode(default)


While changing to landscape (ctrl+f12) will produce a result as



Thank you.

Full Source code Screen Orientation

Comments

Popular posts from this blog

Android Project Structure 2012

Share Data Across Application in Android

Developer Practices - Non Technical 2017