Different guidelines on your smartphone and tablet

5

Hello, I'm 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 separate apps and does not want to leave the unlocked orientation on the smartphone. My solution was to create a bool that changes the value according to the size of the screen, but the application closes on the smartphone when it starts when the device is in the landscape. The main class extends the NoRotateScreen class, the code inside it is:

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

The file that directs the boolean file looks like this:

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

What can I do?

    
asked by anonymous 27.02.2014 / 18:15

2 answers

1

Create a layout in the layout-large or layout-xlarge folder, use the same layout that you use for the phone (same name), but will change the layout id of some componete. 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);
        }
    
27.02.2014 / 18:33
0

Use this method to determine whether the device is a 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;
    }
    
13.03.2014 / 15:23