Introduction

This article shows how to support Landscape and Portrait modes of a device in our applications.
Generally, when we run our application it is displayed in a portrait mode but after changing the layout into portrait mode it will change the display of an application. So this application will have the same display when you run it either in portrait mode or in landscape mode.
It is a very simple concept to support landscape mode or portrait mode in applications. For this, you will need to create a folder named "layout-land". In the layout-land folder, you will paste the same XML file present in your layout folder (activity_main.xml). So when we change the layout to landscape mode it will automatically take the XML file from the layout-land folder.
Step 1
Create an XML file inside the layout folder and write the following:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:orientation="horizontal">
  7. <LinearLayout
  8. android:layout_height="wrap_content"
  9. android:layout_width="wrap_content"
  10. android:orientation="vertical">
  11. <Button
  12. android:id="@+id/button1"
  13. android:layout_height="wrap_content"
  14. android:layout_width="wrap_content"
  15. android:text="@string/about"
  16. android:background="#ffffff">
  17. </Button>
  18. <Button
  19. android:id="@+id/button2"
  20. android:layout_height="wrap_content"
  21. android:layout_width="72dp"
  22. android:text="@string/help"
  23. android:background="#ffffff">
  24. </Button>
  25. </LinearLayout>
  26. <ImageView
  27. android:layout_height="352dp"
  28. android:layout_width="wrap_content"
  29. android:src="@drawable/socialnetwork"
  30. android:paddingLeft="20dp">
  31. </ImageView>
  32. </LinearLayout>
Step 2
Create the same XML file inside the layout-land folder and write the following:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:orientation="horizontal">
  7. <LinearLayout
  8. android:layout_height="wrap_content"
  9. android:layout_width="wrap_content"
  10. android:orientation="vertical">
  11. <Button
  12. android:layout_height="wrap_content"
  13. android:layout_width="wrap_content"
  14. android:text="@string/about"
  15. android:background="#ffffff">
  16. </Button>
  17. <Button
  18. android:layout_height="wrap_content"
  19. android:layout_width="72dp"
  20. android:text="@string/help"
  21. android:background="#ffffff">
  22. </Button>
  23. </LinearLayout>
  24. <ImageView
  25. android:layout_height="352dp"
  26. android:layout_width="wrap_content"
  27. android:src="@drawable/socialnetwork"
  28. android:paddingLeft="20dp">
  29. </ImageView>
  30. </LinearLayout>
Step 3
Create another XML file for another activity.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <TextView
  7. android:layout_height="wrap_content"
  8. android:layout_width="wrap_content"
  9. android:id="@+id/textview"
  10. android:textSize="50dp">
  11. </TextView>
  12. </LinearLayout>
Step 4
Create a Java file and write the following:
  1. package com.supportdifferentscreensizes;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.widget.Button;
  8. public class MainActivity extends Activity
  9. {
  10. Button button1, button2;
  11. @Override
  12. public void onCreate(Bundle savedInstanceState)
  13. {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. button1 = (Button) findViewById(R.id.button1);
  17. button2 = (Button) findViewById(R.id.button2);
  18. button1.setText(R.string.about);
  19. button2.setText(R.string.help);
  20. button2.setOnClickListener(new View.OnClickListener()
  21. {
  22. @Override
  23. public void onClick(View view)
  24. {
  25. Intent i = new Intent(MainActivity.this, Second.class);
  26. Bundle b = new Bundle();
  27. String s = getResources().getString(R.string.help);
  28. b.putString("1", s);
  29. i.putExtras(b);
  30. startActivity(i);
  31. }
  32. });
  33. }
  34. @Override
  35. public boolean onCreateOptionsMenu(Menu menu)
  36. {
  37. // Inflate the menu; this adds items to the action bar if it is present.
  38. getMenuInflater().inflate(R.menu.main, menu);
  39. return true;
  40. }
  41. }
Step 4
Create another Java class file and write the following:
  1. package com.supportdifferentscreensizes;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6. /**
  7. * Created by vivek on 8/26/13.
  8. */
  9. public class Second extends Activity
  10. {
  11. public void onCreate(Bundle savedInstanceState)
  12. {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.second);
  15. TextView textview = (TextView) findViewById(R.id.textview);
  16. Intent i = getIntent();
  17. Bundle b = i.getExtras();
  18. String text = b.getString("1");
  19. textview.setText(text);
  20. }
  21. }
Step 5
Create a string.xml file inside the value-es (res\value-es) folder to support Spanish language as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">SupportDifferentScreenSizes</string>
  4. <string name="action_settings">Settings</string>
  5. <string name="about">acerca de</string>
  6. <string name="help">ayudar</string>
  7. </resources>
Step 6
In the string.xml file inside the value (res\value) folder add support for language as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">SupportDifferentScreenSizes</string>
  4. <string name="action_settings">Settings</string>
  5. <string name="about">About</string>
  6. <string name="help">Help</string>
  7. </resources>
Step 7
Change an Android Manifest.xml file as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.supportdifferentscreensizes"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.supportdifferentscreensizes.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. <activity
  23. android:name=".Second">
  24. </activity>
  25. </application>
  26. </manifest>
Step 8
In portrait mode:
Clipboard02.jpg
In landscape mode:
Clipboard04.jpg