Reputation: 2007
I want to set Portrait Orientation
and lock for ldpi and mdpi screens
. and want to set Landscape orientation and lock for hdpi and xhdpi
.
When app opened in 10 inch tablets
it should be landscape
it cant be portrait
. like that when the app opened in android mobile
it should be in portrait
not landscape
.
So please help me to fix this issue.
Please Refer this Question, its works for me :)
How to check an Android device is HDPI screen or MDPI screen?
switch (getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case DisplayMetrics.DENSITY_MEDIUM:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case DisplayMetrics.DENSITY_HIGH:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case DisplayMetrics.DENSITY_XHIGH:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
Upvotes: 1
Views: 1451
Reputation: 11131
First see here to find whether device is hdpi or mdpi or xhdpi etc etc.
based on that lock your screen orientation.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ldpi || mdpi) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if(hdpi || xhdpi) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
Upvotes: 3