rkaartikeyan
rkaartikeyan

Reputation: 2007

How to set Different Orientation for tablet and mobile in android

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

Answers (1)

Gopal Gopi
Gopal Gopi

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

Related Questions