1
votes

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?

5
You can change orientation by doin setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); are you getting problem in getting screen size ?Rakshit Nawani
First Upall detect device type (Tablet or Phone ) and then set orienation Use :stackoverflow.com/questions/18268218/… & stackoverflow.com/questions/11330363/…Suhas Bachewar

5 Answers

3
votes

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);
}
1
votes

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);
    }
}
1
votes

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);
    }
1
votes

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);
  }
0
votes

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?