Overview
Some useful links are given below to start programming in an Android application.
- Android Programming - Day One
- Android Programming - Day Two
- How To Display A Dialog Windows In Android
- How To Display A Progress Dialog Window In Android
- Intent In Android
- Return Data Using Intent Object In Android Applications
- Passing Data Using An Intent Object In Android Applications
- Fragments In Android Applications
- Adding Fragments Dynamically In Android Application
- Fragment Life Cycle In Android Application
- Interaction Between Two Fragments
- Calling In-Built Functions in Android Application
- Intent Object and Intent Filters in Android Application
- Displaying Notifications in Android Applications
- User Interface in Android Applications
- Orientation, Anchoring, Resizing And Repositioning Of Views In Android Application
- Activity Behavior When Screen Orientation Changes in Android Application
- Persevere with State Information during Changes in Configuration
Introduction
Many times, we want to know the device’s current orientation during runtime. To determine this, we can use the WindowManager class. In this article, we will see how to detect the device orientation and control the orientation of the activity.
Explanation
The code given below demonstrates how to detect the current orientation of your activity.
- import android.view.Display;
- import android.view.WindowManager;
- @Override
- public void onCreate(Bundle bundle)
- {
- Super.onCreate(bundle);
- setContentView(R.layout.main);
- //-----------Get the current display information------
- WindowManager wm = getWindowManager();
- Display display = wm.getDefaultDisplay();
- If (d.getWidth() > d.getHeight()) {
- Log.d(“Orientation is in ”, “Landscape mode”);
- }
- Else
- {
- Log.d(“Orientation is in ”, “Portrait mode”);
- }
- }
Sometimes you want to ensure that your application is displayed in only a certain orientation. For example, you may be writing a game that should be viewed only in landscape mode. In this case, you must programmatically force a change in the orientation, using the setRequestOrientation() method of the Activity class.
- Import android.content.pm.ActivityInfo;
- @Override
- public void onCreate(Bundle bundle)
- {
- Super.onCreate(bundle);
- setContentView(R.layout.main);
- //-----------Get the current display information------
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
- }
If you want to change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_PORTRAIT constraint. Apart from the method given , you can also use the android:screenOrientation attribute on the <activity> element in the AndroidManifest.xml as follows to constrain the activity to a certain orientation.
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.administrator.orientationapp" >
- <uses-sdk android:minSdkVersion=”18”/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".MainActivity"
- android:screenOrientation="landscape"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
The preceding example constrains the activity to a certain orientation (landscape in this case) and prevents the activity from being destroyed. The activity will not be destroyed and the onCreate() method will be fired again when the orientation of the device changes.
Conclusion
In this article, you learned how to detect the orientation changes and control the orientation of the activity. In the next article, I will explain about utilizing the Action Bar.

Join the conversation! Your thoughts help the community grow.