Reputation: 173
In android it is possible to lock the screen orientation by adding this to the manifest :
android:screenOrientation="landscape"
But is it possible to lock within the size?
I want my app to be locked in portrait for phone format, and I want the user beeing able to use both portrait and landscape on a tablet format.
I've tried with screen size by using :
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
but it doesn't seem to work when I want to lock screen from code. Any idea of a good way to do this?
Upvotes: 1
Views: 1284
Reputation: 11873
You can create a custom method to check the current screen density of phone,
public static boolean isTabLayout(Activity activity) {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float scaleFactor = metrics.density;
float widthDp = widthPixels / scaleFactor;
float heightDp = heightPixels / scaleFactor;
float smallestWidth = Math.min(widthDp, heightDp);
if (smallestWidth > 720) {
//Device is a 10" tablet
return true;
} else if (smallestWidth > 600) {
//Device is a 7" tablet
return true;
}
return false;
}
To lock the screen orientation in portrait,
if (!isTabLayout(this)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
or in landscape,
if (!isTabLayout(this)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
Upvotes: 1
Reputation: 2604
You can get the diagonal Value of the device by following
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
float yInches= metrics.heightPixels/metrics.ydpi;
float xInches= metrics.widthPixels/metrics.xdpi;
double diagonalInches = Math.sqrt(xInches*xInches + yInches*yInches);
After that you can check according to your need if you want the size of 5,5.5,6,6.5 or higher and change orientation according to need
if (diagonalInches>=6.5){
// 6.5inch device or bigger
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else{
// smaller device
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
Upvotes: 1
Reputation: 12379
Use This:
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
public void setOrientation(Context context){
if(!isTablet(contex){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
Upvotes: 0
Reputation: 522
I don't know exactly its working or not try this
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE){
return "Tablet";
}else{
return "Mobile";
}
or look this once Determine if the device is a smartphone or tablet?
Upvotes: -1
Reputation: 5097
first check this answer
now you can change orientation like this
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (!tabletSize) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Upvotes: 4