Skip to content Skip to sidebar Skip to footer

How To Make Application Run Portrait In Phone And Landscape On Tablets

Hi I am looking to learn about these so that I can give more good layouts and UI exp of my application. I want my app must run only in portrait mode in case of phone that I can set

Solution 1:

Create a new file named bools.xml in values folder as below:

<?xml version="1.0" encoding="utf-8"?><resources><itemtype="bool"name="isLargeLayout">false</item></resources>

create another file in values-large folder named bools.xml as below:

<?xml version="1.0" encoding="utf-8"?><resources><itemtype="bool"name="isLargeLayout">true</item></resources>

Now in your activity before calling to setContentView get this resource and based on its value decide port or land orientation:

boolean isLargeLayout = getResources().getBoolean(R.bool.isLargeLayout);
if(isLargeLayout) {
    // Tablet Mode
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
    // Handset Mode
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

Solution 2:

Use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) to set it programatically;

You can get the screen dimensions like this this:

Displaydisplay= getWindowManager().getDefaultDisplay();
Pointsize=newPoint();
display.getSize(size);
intwidth= size.x;
intheight= size.y;

If you're not in an Activity you can get the default Display via WINDOW_SERVICE:

WindowManagerwm= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Displaydisplay= wm.getDefaultDisplay();


Displaydisplay= getWindowManager().getDefaultDisplay(); 
intwidth= display.getWidth();  // deprecatedintheight= display.getHeight();  // deprecated

Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated.

Post a Comment for "How To Make Application Run Portrait In Phone And Landscape On Tablets"