57

Is it possible simple to detect current orientation of android device, without programming a listener and handling the position matrix? In my app I want only to know current orientation - vertical or horizontal - at the moment. However I don't want to listen to events of axiometry or other events.

Cristian
  • 198,401
  • 62
  • 356
  • 264
Mark
  • 17,887
  • 13
  • 66
  • 93

6 Answers6

76

You can also use:

getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
biegleux
  • 13,179
  • 11
  • 45
  • 52
  • 1
    I know this is a bit of an older post, but in case one's working with a compass, the device must know the exact rotation to add or substract the correct value to get the correct bearing, so your anwser isn't the best, yet still useful. – DaMachk Aug 09 '13 at 08:29
  • This doesn't work on Samsung Galaxy S5. Best solution is Cristian's answer. – Hasanaga Feb 14 '18 at 09:31
72

Use the getRotation method:

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

From the documentation:

Returns the rotation of the screen from its "natural" orientation. The returned value may be Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.ROTATION_180, or Surface.ROTATION_270. For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned. The angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device. For example, if the device is rotated 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the returned value here will be Surface.ROTATION_90.

Keep in mind that getRotation was introduced from Android 2.2. Use getOrientation if your target are older devices.

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • 5
    did it the job if user switch off the rotation? For example I have switch off the rotation, so my desktop orientation is always portrait, but the camera app, rotate the preview independet of this configuration. – Mark Feb 25 '11 at 00:20
  • 1
    This works on my phone - Galaxy S3, but NOT on my 10 inch tablet emulator with Android 4.3: getRotation() returns exactly opposite rotation than the actual one (landscape - portrait)! – Yar Jun 04 '15 at 17:08
  • @Yar May be your tablet emulator's starting orientation is in portrait so this code gives you exactly opposite results. Follow biegleux's answer instead – ChaturaM Apr 17 '18 at 02:48
4

I think the safest and easiest way is to add a tag for some element of your activity in XML. For example, set the viewpager's tag to "portrait" in the portrait layout and set it to "landscape" in in landscape. Then in your oncreate check for that tag like so:

if(mViewpager.getTag().equals("portrait"))
  // is in portrait
else
  // is in landscape
9patchcoder
  • 619
  • 1
  • 5
  • 17
1

You just need to know the height and the width of your canvas... your surface... your monitor... etc.

maybe you can get it with :

if (canvas.getHeight()>canvas.getWidth() ) {
//portrait
}
else
{
//landscape
}
ORParga
  • 11
  • 2
  • well, i would not depend on such a thing. What `canvas` is this? Is it in a layout, what are it's layout parameters and who uses the layout in what situations... Way too many questions with a 'W' for a functionality that seems quite hardware-bound. – Bondax Jun 02 '15 at 12:07
1

You could make an extension function from the code above:

fun AppCompatActivity.getRotation(): Int{
    val display = (baseContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
    val rotation = display.rotation
    when(rotation){
        Surface.ROTATION_90 -> return R.integer.LANDSCAPE
        Surface.ROTATION_270 -> return R.integer.LANDSCAPE
        Surface.ROTATION_180 -> return R.integer.PORTRAIT
        Surface.ROTATION_0 -> return R.integer.PORTRAIT
        else ->
            return R.integer.PORTRAIT
    }
}

In your resource directory create a constants.xml file with the following contents:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="LANDSCAPE">0</integer>
    <integer name="PORTRAIT">1</integer>
</resources>

You code is then as easy as (e.g. vertical / horizontal layout for a Recyclerview in a Fragment:

val a  = activity as AppCompatActivity
        val orientation = a.getRotation()
        when (orientation){
            R.integer.PORTRAIT -> rv.layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL,false)
            R.integer.LANDSCAPE -> rv.layoutManager = LinearLayoutManager(activity, RecyclerView.HORIZONTAL,false)
        }
Jens Buysse
  • 119
  • 10
0

This may not be relevant, but if by chance you need to know this because you want to lock the device into its’ current orientation you can toggle between the following two lines of code.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
user2288580
  • 2,210
  • 23
  • 16