Different orientations on smartphone and tablet

Asked

Viewed 332 times

5

Hello, I am developing an app and I need the screen to be in Portrait for smartphones and Portrait/Landscape for tablets. The customer does not want to accept different applications and does not want to leave the orientation unlocked on the smartphone. My solution was to create a bool that changes the value according to the screen size, but the application closes on the smartphone when starting it when the device is in Landscape. The main class extends the Norotatescreen class, the code within it is:

   boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
   if (!tabletSize) {
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
   }

The file that arrow the Boolean file is like this:

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

What can I do?

  • Makes a layout for mobile and another for tablet, then makes an if to see if eh a tablet or a cell phone.

  • The client does not want to accept.

  • Peter take a look at my answer see if you understand

2 answers

2

Create a layout in the folder layout-large or in the layout-xlarge, use the same layout you use for the mobile (same name), but will change the id of some piece. then in the code you do something like:

        // tablet
        if (findViewById(R.id.menu) != null) {
            // esse id.menu só vai existir no layout com tablet
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
  • Tested here worked, any questions may ask.

  • Out of context.

  • 1

    As well as out of context ??

  • Well I showed you an example of how to differentiate a tablet from a mobile phone, and make the tablet can stay Both Landscape as Portrait, and the mobile only Portrait; which from what I understood was what you wanted...

  • I solved the problem, during the splash screen the application is released the rotation, after that, it blocks already on the login screen.

0

Use this method to know if the device is tablet:

public static boolean isTabletDevice(final Context ct) {
        if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb
            /**
             * testa o tamanho da tela usando reflection porque isLayoutSizeAtLeast
             * só está disponível a partir do skd int 11
             */
            Configuration con = ct.getResources().getConfiguration();
            try {
                Method mIsLayoutSizeAtLeast = con.getClass().getMethod(
                        "isLayoutSizeAtLeast", int.class);
                Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con,
                        0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
                return r;
            } catch (Exception x) {
                return false;
            }
        }
        return false;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.