Reputation: 34517
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.
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.
Upvotes: 0
Views: 1682
Reputation: 9044
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);
}
Upvotes: 1
Reputation: 14499
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.
Upvotes: 0