Detect Orientation Changes And Control The Orientation Of The Activity

Overview

Some useful links are given below to start programming in an Android application.

  1. Android Programming - Day One
  2. Android Programming - Day Two
  3. How To Display A Dialog Windows In Android 
  4. How To Display A Progress Dialog Window In Android
  5. Intent In Android
  6. Return Data Using Intent Object In Android Applications
  7. Passing Data Using An Intent Object In Android Applications 
  8. Fragments In Android Applications
  9. Adding Fragments Dynamically In Android Application
  10. Fragment Life Cycle In Android Application
  11. Interaction Between Two Fragments
  12. Calling In Built Functions in Android Application
  13. Intent Object and Intent Filters in Android Application
  14. Displaying Notifications in Android Applications
  15. User Interface in Android Applications
  16. Orientation, Anchoring, Resizing And Repositioning Of Views In Android Application
  17. Activity Behavior When Screen Orientation Changes in Android Application
  18. 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. 

  1. import android.view.Display;  
  2.   
  3. import android.view.WindowManager;  
  4.   
  5. @Override  
  6. public void onCreate(Bundle bundle)  
  7. {  
  8.   
  9.      Super.onCreate(bundle);  
  10.   
  11.      setContentView(R.layout.main);  
  12.   
  13. //-----------Get the current display information------  
  14.   
  15.      WindowManager wm = getWindowManager();  
  16.   
  17.      Display display = wm.getDefaultDisplay();  
  18.   
  19.      If (d.getWidth() > d.getHeight()) {  
  20.   
  21.          Log.d(“Orientation is in ”, “Landscape mode”);  
  22.   
  23.      }  
  24.   
  25.      Else  
  26.   
  27.      {  
  28.   
  29.          Log.d(“Orientation is in ”, “Portrait mode”);  
  30.   
  31.      }  
  32.   
  33. }   

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.

  1. Import android.content.pm.ActivityInfo;  
  2.   
  3. @Override  
  4. public void onCreate(Bundle bundle)  
  5. {  
  6.   
  7.     Super.onCreate(bundle);  
  8.   
  9.     setContentView(R.layout.main);  
  10.   
  11.       //-----------Get the current display information------  
  12.     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
  13.   
  14.   
  15. }   

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.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.      package="com.example.administrator.orientationapp" >  
  4.       
  5.     <uses-sdk android:minSdkVersion=”18”/>  
  6.        
  7.     <application  
  8.          android:allowBackup="true"  
  9.          android:icon="@mipmap/ic_launcher"  
  10.          android:label="@string/app_name"  
  11.          android:theme="@style/AppTheme" >  
  12.          <activity  
  13.              android:name=".MainActivity"  
  14.             android:screenOrientation="landscape"  
  15.              android:label="@string/app_name" >  
  16.              <intent-filter>  
  17.                  <action android:name="android.intent.action.MAIN" />  
  18.    
  19.                  <category android:name="android.intent.category.LAUNCHER" />  
  20.              </intent-filter>  
  21.          </activity>  
  22.      </application>  
  23.    
  24.  </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.

Up Next
    Ebook Download
    View all
    Learn
    View all