55

Is there anyway to find out if a device is portrait or landscape by default? In that I mean how you normally use the device.

Most phones have a portrait screen for normal usage but is there some flag for finding that out?

joynes
  • 1,627
  • 4
  • 16
  • 19
  • Do you mean to find on application run time to check weather device is landscape or portrait? – Herry May 21 '11 at 11:22
  • possible duplicate of [How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or Flipout)](http://stackoverflow.com/questions/4553650/how-to-check-device-natural-default-orientation-on-android-i-e-get-landscape) – weston May 13 '14 at 11:24

9 Answers9

179

You can do this by:

For Lanscape

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    // Do some stuff
}

For Portrait

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    // Do some stuff
}

Check: Configuration.orientation

hata
  • 11,633
  • 6
  • 46
  • 69
Hazem Farahat
  • 3,790
  • 2
  • 26
  • 42
10

It's simple, Just a If-Else block:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    // landscape
} else {
    // portrait
}
Sheikh Hasib
  • 7,423
  • 2
  • 25
  • 37
3

Thanks to @Codeversed, whose excellent Answer is very close to working (but does NOT as shown), I have a MainActivity that works well. All that need be done is move .enable to where it can be executed OUTSIDE the on block, such as immediately after declaration of myOrientationEventListener.

import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.OrientationEventListener;

public class MainActivity extends Activity
{
    public static boolean PORTRAIT_MODE = true;
    OrientationEventListener myOrientationEventListener ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        myOrientationEventListener = new OrientationEventListener(this,
                SensorManager.SENSOR_DELAY_NORMAL)
        {
            @Override
            public void onOrientationChanged(int orientation)
            {
                PORTRAIT_MODE = ((orientation < 100) || (orientation > 280));
                Log.w("Orient", orientation + " PORTRAIT_MODE = " + PORTRAIT_MODE);
            }
        };
        Log.w("Listener", " can detect orientation: " + myOrientationEventListener.canDetectOrientation() + " ");
        myOrientationEventListener.enable();
    }
}
DSlomer64
  • 4,234
  • 4
  • 53
  • 88
3
package com.android.portraitandlandscape;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.widget.Toast;

public class PortraitLandScape extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();

    Log.v("log_tag", "display width is "+ width);
    Log.v("log_tag", "display height is "+ height);

    if(width<height){
        Toast.makeText(getApplicationContext(),"Device is in portrait mode",Toast.LENGTH_LONG ).show();
    }
    else{
        Toast.makeText(getApplicationContext(),"Device is in landscape  mode",Toast.LENGTH_LONG ).show();
    }

 }
}

You can try this code for checking whether device is in landscape or in portrait.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
Herry
  • 7,037
  • 7
  • 50
  • 80
2

In your actvity, do this:

if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

for example...

Cícero Moura
  • 2,027
  • 1
  • 24
  • 36
  • won't work if you have `android:screenOrientation="portrait|landscape"` set in the manifest on the Activity. – Pierre Sep 21 '19 at 12:08
1

I had a similar issue. Solved it by comparing values of rotation and orientation. There is no need to use sensors or any listeners, just call isDefaultLandscape method whenever you wish.

private boolean isDefaultLandscape(final Context context)
{
    Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int rotation = display.getRotation();
    int orientation = context.getResources().getConfiguration().orientation;

    switch (rotation)
    {
        case Surface.ROTATION_180:
        case Surface.ROTATION_0:
        {
            return orientation == Configuration.ORIENTATION_LANDSCAPE;
        }
        default:
        {
            return orientation == Configuration.ORIENTATION_PORTRAIT;
        }
    }
}
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
1

I am not sure if this will help but this is a quick example I wrote that will show you how to have a listener...which will allow you to figure out which orientation you are in.

...textviewOrientation = (TextView)findViewById(R.id.textView1);

    myOrientationEventListener = new OrientationEventListener(this, 
              SensorManager.SENSOR_DELAY_NORMAL){

        @Override
        public void onOrientationChanged(int arg0) {

         textviewOrientation.setText("Orientation: " + String.valueOf(arg0));
         myOrientationEventListener.enable();


             if ((arg0 < 100) || (arg0 > 280)){

                 setRequestedOrientation(
                     ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

             } else {

                 setRequestedOrientation(
                     ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

             }


        }

    };...


...@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    myOrientationEventListener.disable();
}...
Codeversed
  • 9,287
  • 3
  • 43
  • 42
0
// Current orientation
public boolean landscape = false;

public boolean isLandscape(){

    DisplayMetrics displaymetrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int width = displaymetrics.widthPixels;
    int height = displaymetrics.heightPixels;

    if(width<height){
        landscape = false;
    }
    else{
        landscape = true;
    }

    return landscape;

}
Papa Yev
  • 618
  • 7
  • 10
0
private boolean isPortraitMode() {
    int orientation = context.getResources().getConfiguration().orientation;
    if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
      return false;
    }
    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
      return true;
    }
    Log.d(TAG, "isPortraitMode returning false by default");
    return false;
  }
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 26 '23 at 22:14