0

Hi I am looking to learn about these so that I can give more good layouts and UI exp of my application. I want my app must run only in portrait mode in case of phone that I can set in the AndroidManifest.xml like

    <activity
        android:name=".SplashActivity"
        android:label="@string/title_activity_splash"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

But This will keep for all phones and tablet. If I do this way then of course my layout won't look good on 7 and 10 inch screens.

Question

How to set such that my app run in portrait mode in mobile phones and landscape mode on 7 and 10 inch screens.

Thanks in advance.

N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • possible duplicate of [How do I make it only landscape for tablets and only portrait for phones?](http://stackoverflow.com/questions/10153992/how-do-i-make-it-only-landscape-for-tablets-and-only-portrait-for-phones) – jbutler483 Nov 19 '14 at 15:15
  • @jbutler483 How this you can get whether device is 7 or 10 inch ? – N Sharma Nov 19 '14 at 15:18
  • it was answered in the accepted answer: [here](http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels) or even [this](http://stackoverflow.com/questions/2193457/is-there-a-way-to-determine-android-physical-screen-height-in-cm-or-inches) – jbutler483 Nov 19 '14 at 15:19

2 Answers2

1

Create a new file named bools.xml in values folder as below:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
    <item type="bool" name="isLargeLayout">false</item>
</resources>

create another file in values-large folder named bools.xml as below:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
    <item type="bool" name="isLargeLayout">true</item>
</resources>

Now in your activity before calling to setContentView get this resource and based on its value decide port or land orientation:

boolean isLargeLayout = getResources().getBoolean(R.bool.isLargeLayout);
if(isLargeLayout) {
    // Tablet Mode
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
    // Handset Mode
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106
0

Use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) to set it programatically;

You can get the screen dimensions like this this:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

If you're not in an Activity you can get the default Display via WINDOW_SERVICE:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();


Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated.

Machado
  • 14,105
  • 13
  • 56
  • 97
  • THe recommended way to do this is use distinct resources for each. You can design layouts for portrait or landscape, large size devices, small, etc. For example you can have a layout exist in res/layout-port and in res/layout-landscape and in res/layout-sw600dp, which is good for 7" tablets – Martin Nov 19 '14 at 15:39