Getting orientation of Android device
Solution 1
Here is the implementation for getOrientation()
:
public int getscrOrientation()
{
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = getOrient.getOrientation();
// Sometimes you may get undefined orientation Value is 0
// simple logic solves the problem compare the screen
// X,Y Co-ordinates and determine the Orientation in such cases
if(orientation==Configuration.ORIENTATION_UNDEFINED){
Configuration config = getResources().getConfiguration();
orientation = config.orientation;
if(orientation==Configuration.ORIENTATION_UNDEFINED){
//if height and widht of screen are equal then
// it is square orientation
if(getOrient.getWidth()==getOrient.getHeight()){
orientation = Configuration.ORIENTATION_SQUARE;
}else{ //if widht is less than height than it is portrait
if(getOrient.getWidth() < getOrient.getHeight()){
orientation = Configuration.ORIENTATION_PORTRAIT;
}else{ // if it is not any of the above it will definitely be landscape
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
}
}
return orientation; // return value 1 is portrait and 2 is Landscape Mode
}
And you can also refer this example which represent the use of both the methods:
getOrientation and getRotationMatrix
http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html
Solution 2
public int getScreenOrientation() {
// Query what the orientation currently really is.
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
return 1; // Portrait Mode
}else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
return 2; // Landscape mode
}
return 0;
}
Solution 3
protected void onResume() {
// change the screen orientation
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.portrait);
} else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.landscape);
} else {
setContentView(R.layout.oops);
}
}
Related videos on Youtube
Johann
Medium Articles Creating responsive layouts with Jetpack Compose https://johannblake.medium.com/creating-responsive-layouts-using-jetpack-compose-7746ba42666c Create Bitmaps From Jetpack Composables https://proandroiddev.com/create-bitmaps-from-jetpack-composables-bdb2c95db51 Navigation with Animated Transitions Using Jetpack Compose https://proandroiddev.com/navigation-with-animated-transitions-using-jetpack-compose-daeb00d4fb45 In-App Testing For Android https://proandroiddev.com/in-app-testing-for-android-6f762bb97387 Github https://github.com/JohannBlake ANDROID DEVELOPMENT 10 years of native Android development with Kotlin, Java & Android Studio Design, develop, test and deploy Android applications Professionally looking UIs with Jetpack Compose & Material Design Develop using continuous integration APIs: Google Cloud Messaging, Google Maps, Google Drive, Gmail, OAuth Frameworks & Patterns: MVVM, RxJava, Dagger, Koin, Retrofit, Sqlite, Room, Realm, Crashlytics Communicate with web services via RESTful APIs Troubleshoot, optimize and performance tune Code versioning using Git and the Gitflow model Project management with Jira Agile development with Scrum WEB DEVELOPMENT 25 years of web development React, Material-UI Javascript, HTML5, CSS3, jQuery, ASP.NET, C#, Java, Servlets Microservices running on the Google Cloud Platform ELECTRON DEVELOPMENT 2 Years of Electron development Javascript, jQuery Node.js HTML5, CSS3 Published Apps Motel One https://play.google.com/store/apps/details?id=com.motelone.m NBC Sports https://play.google.com/store/apps/details?id=air.com.nbcuni.com.nbcsports.liveextra Telemundo Noticias https://play.google.com/store/apps/details?id=com.nbcuni.telemundo.noticiastelemundo TD Mobile Banking https://play.google.com/store/apps/details?id=com.td
Updated on September 16, 2022Comments
-
Johann over 1 year
You would think that there would be a straight forward solution. The Android docs state:
The orientation sensor was deprecated in Android 2.2 (API level 8). Instead of using raw data from the orientation sensor, we recommend that you use the getRotationMatrix() method in conjunction with the getOrientation() method to compute orientation values.
Yet, they don't provide a solution on how to implement
getOrientation()
andgetRotationMatrix()
. I've spent several hours reading through posts here on developers using these methods but they all have partially pasted code or some weird implementation. Googling hasn't provided a tutorial. Can someone please paste a simple solution using these two methods to generate the orientation?? -
Johann about 11 yearsThat gets the screen orientation which I already use in code. I don't believe that that is the same thing as getting the device orientation based upon sensors. In fact, I even tried that in the past and it won't work inside a service with no screen. Services don't usually access the screen and should be able to determine the device orientation from the sensors. The getOrientation and getRotationMatrix are required.
-
Johann about 11 yearsI tried it out but it doesn't give the results as one would expect. I just downloaded an app from the app store that shows all the sensors on your device and how they operate. One is called the Orientation sensor and when you use it, the app shows how the sensor correctly works. Values should not change (or change very little) when you don't move the device. The code in the link above doesn't give any reliable results but the values jump all over the place.
-
Johann about 11 yearsI'm beginning to think that maybe putting a breakpoint and running in debug mode may be affecting the way the sensors get read. I'll try and run it without debug and display the values as they get updated and see what happens.
-
Johann about 11 yearsThe code in the link you provided does work but I discovered that it will fail if you put a breakpoint in at the point where the orientation is calculated. However, if you let the app run for a few seconds and then press F9 on the line where the calculation is done, it will produce the correct values. Debugging seems to interfere with how the sensors operate. Also, it is vital to check that getRotationMatrix returns true before doing any calculation.
-
Nipun Gogia about 11 yearsmeans this link seems to be helpful for u
-
Denis Gladkiy over 10 yearsDisplay.getOrientation has been depricated in API Level 8. Display.getRotation should be used instead.