Paul Russell
Paul Russell

Reputation: 1

Android: Locking screen orientation sometimes

I have an application that is designed for tablets. It displays one view of its data in portrait mode and a different view of the data in landscape. The user can switch between the two at will.

On a smaller "phone" type device, landscape is too small to be useful, but portrait mode is still viable. I would like to force portrait on a smaller device, say anything under 7, while allowing both modes on tablets.

How do I go about this?

Upvotes: 0

Views: 391

Answers (2)

cYrixmorten
cYrixmorten

Reputation: 7108

Perhaps Krylez is right and the correct solution is to do something else but in case you want to lock/release screen orientation anyway (I use it together with short running AsyncTasks to avoid having to deal with the ProgressDialog)

public class Device {

public static void lockOrientation(Activity activity) {
    Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int rotation = display.getRotation();
    int tempOrientation = activity.getResources().getConfiguration().orientation;
    int orientation = 0;
    switch(tempOrientation)
    {
    case Configuration.ORIENTATION_LANDSCAPE:
        if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        else
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        else
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
    }
    activity.setRequestedOrientation(orientation);
}





public static void releaseOrientation(Activity activity) {
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}

}

Upvotes: 1

Krylez
Krylez

Reputation: 17820

You should follow the developer guidelines for supporting multiple screens. You can then use the sw qualifier to switch on the physical screen size.

Upvotes: 1

Related Questions