Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android determine screen orientation at runtime

Tags:

Here's a pseudo code to detect screen rotate event, and decide to retain or changes the screen orientation.

public boolean onOrientationChanges(orientation) {   if(orientation == landscape)     if(settings.get("lock_orientation"))       return false;   // Retain portrait mode     else       return true; // change to landscape mode    return true;  } 

How do I make similar things in Android?

EDIT: I'm actually looking answer on Where to handle orientation changes. I do not want to fix the orientation by adding screenOrientation="portrait".

I need something, similar to onConfigurationChanges(), where I can handle the orientation, but do no need me to manually redraw the view.

like image 534
Yong Fei Avatar asked Feb 29 '12 23:02

Yong Fei


People also ask

How can we detect the orientation of the screen in Android?

int orientation = display. getOrientation(); Check orientation as your way and use this to change orientation: setRequestedOrientation (ActivityInfo.

How can we detect the orientation of the screen?

You can detect this change in orientation on Android as well as iOS with the following code: var supportsOrientationChange = "onorientationchange" in window, orientationEvent = supportsOrientationChange ? "orientationchange" : "resize"; window.


2 Answers

You need a Display instance firstly:

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 

Then orientation may be called like this:

int orientation = display.getOrientation(); 

Check orientation as your way and use this to change orientation:

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

I hope it helps.

Update:

Okay, let's say you've an oAllow var which is Boolean and default value is False.

public void onConfigurationChanged(Configuration newConfig) {     super.onConfigurationChanged(newConfig);     Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();     int orientation = display.getOrientation();      switch(orientation) {         case Configuration.ORIENTATION_PORTRAIT:             if(!oAllow) {                     setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);             }             break;         case Configuration.ORIENTATION_LANDSCAPE:             if(!oAllow) {                     setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);             }             break;     } } 

You can add more choices.

I didn't try this sample, but at least tells you some clues about how to solve. Tell me if you got any error.

UPDATE

getOrientation() is already deprecated see here. Instead Use getRotation(). To check if the device is in landscape mode you can do something like this:

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))         .getDefaultDisplay();  int orientation = display.getRotation();  if (orientation == Surface.ROTATION_90         || orientation == Surface.ROTATION_270) {     // TODO: add logic for landscape mode here             } 
like image 136
Ogulcan Orhan Avatar answered Oct 01 '22 03:10

Ogulcan Orhan


Try running

getResources().getConfiguration().orientation 

From your context object to figure out what is the screen orientation at runtime, the possible values are documented here

In order to catch the orientation change event you can find the answer in the Android Dev Guide: Handling the Configuration Change Yourself

From the guide :

For example, the following manifest code declares an activity that handles both the screen orientation change and keyboard availability change:

<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">

Now, when one of these configurations change, MyActivity does not restart. Instead, the MyActivity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your activity.

...

like image 38
Talihawk Avatar answered Oct 01 '22 02:10

Talihawk