1

I have an Activity A which has 2 types of layout. Phones have a single layout and tablets have a Master-Detail layout. The Master-Detail layout is for 7 inch landscape and 10 inch tablets. I have used following layout qualifiers:

-layout - for phones
-layout-sw720dp - for 10 inch tablet
-layout-w820dp-land - for 7 inch tablet landscape
-layout-h820dp - for 7 inch tablet portrait

Untill now everything is working fine.

The problem:

Now I want the activity to work only in portrait mode for phones and in both orientation in 7 and 10 inch tablets and for that I have used the below resources and size qualifiers from this link https://stackoverflow.com/a/14793611/373889

for res/values
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
            <bool name="portrait_only">true</bool>
        </resources>

for res/values-sw720dp
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
            <bool name="portrait_only">false</bool>
        </resources>

for res/values-w820dp-land
        <?xml version="1.0" encoding="utf-8"?>
        <resources>
                <bool name="portrait_only">false</bool>
            </resources>

for res/values-h820dp
        <?xml version="1.0" encoding="utf-8"?>
        <resources>
                <bool name="portrait_only">false</bool>
            </resources>

Now the issue is, in the 7 inch tablet, if started in portrait mode, shows normal phone layout in both orientation and if started in landscape mode, shows Master-Detail layout in both orientation. It is not working as it should work. The phones are working fine with normal layout and 10 inch tablet working fine with Master-Detail layout in both direction.

Please let me know if there is something wrong with this approach. Thank you.

Community
  • 1
  • 1
prasanjitDe
  • 179
  • 1
  • 7

1 Answers1

1

Well from my experience, I think that you should do a layout for each screen size, and I think you did this. After setonContentView(...) do this:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

And based on the height and the width of the screen lock or not the orientation of the device. To lock you can add those lines:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

And for not locking the screen don't add anything related to the screen orientation in the android manifest or activity, rotation is enabled by default.

I hope it works, let me know.

Ankush Bist
  • 1,862
  • 1
  • 15
  • 32
  • 1
    I have actually used this on onCreate(); `if(getResources().getBoolean(R.bool.portrait_only)){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }` but the issue is it keeps the same layout for both orientation whereas I need normal layout for portrait and Master-Detail layout for landscape. – prasanjitDe Jul 01 '16 at 13:34