-1

I am new to android. I am still in learning mode. I have been asked to develop an app with the first requirement that on Phone the app should be only portrait and in tablets it should only be landscape.

I have developed the app for portrait mode by usual default folder layout and in manifest I am setting as android:screenOrientation="portrait"

Now for landscape I thought If I create folder with like "layout-sw600dp-land" will automatically call xml from this folder for tablets but then realized default orientation has been provided in manifest so it will all be portrait always.

So now question is how do I achieve phone (Portrait - Landscape should not happen) and tablet(Landscape - portrait should not happen)

Do I have to create the same Java & Layout (XML) for landscape (setting in manifest) or is there easy way in changing folders only?

Sanjana Nair
  • 2,663
  • 6
  • 26
  • 44
  • 1
    check this link http://stackoverflow.com/questions/2124046/how-do-i-specify-different-layouts-for-portrait-and-landscape-orientations – Karthik Rk Aug 14 '15 at 05:00
  • also check this link http://developer.android.com/training/basics/supporting-devices/screens.html – Karthik Rk Aug 14 '15 at 05:03

3 Answers3

1

Okay, the solution I'm going to propose may not be the perfect solution, but I am certain it'll work.

It'll rely on one assumption though :

  • The tablet's screen MUST be larger than the phone's screen.

First get the X and Y size in pixels :

WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);

double width = size.x;
double height = size.y;

Now, all you have to do is find the height and width of a standard tablet, and if it is greater than or equal to the height and width of a regular tablet, then you'll do :

if(width > normalTabletWidth && height > normalTabletHeight){
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Henry98
  • 571
  • 2
  • 12
1

you should musy use getDisplay() of android... usually tablets have 6.5 or 7 inch or greater display size and phones have smaller display size than 6.5 inches... so compare dispaly properties and then decide to change screen mode to landscape or portrait...

0

I was also facing the same issue, then I found a solution to it.

First you have to declare layout folders for your tablet, for example:

layout-sw360dp/ : 720p screens 

and layout-sw540dp/ : 1080p screens

Then, in these folder define your layout files. But it won't fix the portrait and landscape issue.

In order to make your app responsive you have to decrale some XML files also.

Put a bool resource in res.values to check for screen size.

bool.xml (mobile)
 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="portrait_only">true</bool>
    <bool name="landscape_only">false</bool>
</resources>

// bool.xml (sw600dp)
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="portrait_only">false</bool>
    <bool name="landscape_only">true</bool>
</resources>

// bool.xml (sw720dp)
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="portrait_only">false</bool>
        <bool name="landscape_only">true</bool>
    </resources>

After adding these resources go to you MainActivity.java file and above:

setContentView(R.layout.activity_main);

Add this check:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // locking out landscape screen orientation for mobiles
        if(getResources().getBoolean(R.bool.portrait_only)){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        // locking out portait screen orientation for tablets
        if(getResources().getBoolean(R.bool.landscape_only)){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        }

        setContentView(R.layout.activity_main);
}

For changing the orientation correctly, you should add following to the activity tag in manifest:

<activity 
    android:name="com.ABC"
    android:configChanges="keyboardHidden|orientation|screenSize">
</activity>

This will do.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
shaheryar
  • 43
  • 6