Android dynamic UI for phones and tablets

6/26/2018 posted in  Android comments

This is mainly for Phone UI and Tablet UI. Since Android has supported split screen since Android OS 7.0. It's not suitable to use isTablet to check whether App runs on a tablet. It might be a tablet but shows a phone UI.

0x00 Phones and Laptops

The common way to distinguish between phones and tablets is using screen size. The default boundary is 600dp. If any of the device width and height is higher than 600dp, than it's a tablet.

Devices Screen size
Phones < sw600dp
Tablets > sw600dp

0x01 Differences between isTablet and isTabletLayout

  • isTablet

    This method is used to check Application context, will never changed after app starts. For example: if you start an app on a tablet, this will always return true, whatever you do with the app or tablet.

  • isTabletLayout

    This method is used to check current layout for the app. Will change dynamically when the app window size change. For example: if you split screen and make this app runs in a smaller screen, you will get a phone size screen for the app, then this method will return false.

App windows size

For a tablet that OS version higher then 7.0, screen size for App can be in this three types:

  1. Landscape
+----------------+
|                |
|      App       |
|                |
+----------------+
  1. Portrait
+----------+
|          |
|          |
|   App    |
|          |
|          |
+----------+

  1. Split screen

    1. Phone size
    +-----+-----------+
    |     |           |
    | App |           |
    |     |           |
    +-----+-----------+
    
    1. Tablet landscape
    +-----+-----------+
    |     |           |
    |     |    App    |
    |     |           |
    +-----+-----------+
    
    1. Tablet portrait
    +--------+--------+
    |        |        |
    |  App   |        |
    |        |        |
    +--------+--------+
    

Of course, for split screen, Tablet landscape and portrait are the same as a smaller size of a tablet, but Phone-size is more like a normal phone.

So, on tablet, we have to detect whether it's a tablet layout(no matter landscape or portrait) or a phone layout.

0x02 How to do

To detect current layout or context, we can use a small trick, R.bool.tablet, add this value to res/values/bool.xml and res/values-sw600dp/bool.xml and res/values-sw720dp/bool.xml and set the first value as false, follow two value as true.

In this way, when you try to get value of this bool value, you can know whether you are using a table layout or not.

And, if we need to get current context of app, we should use isTablet method, and use Application context to get the value.

In this case, on tablet, this method will always return true, no matter what layout you are using.

public static boolean isTablet() {
  return getApplicationContext().getResources().getBoolean(R.bool.tablet);
}

If we need to get current layout, we can use isTabletLayout method.

public static boolean isTabletLayout(Context c) {
  if (c == c.getApplicationContext) {
    throw new IllgalArgumentException("Must not call this method using Application context")
  }
  return c.getResources().getBoolean(R.bool.tablet);
}

We must ensure that this method is not called on Application context, because if this happens, this is actually isTablet method, and will return true even if you are using a phone layout on a table.

That is all for Android dynamic UI for tablet layout :-)

Hope you enjoy it.

0x03 Something more...

With the change of screen size of phones, 16:9 is not the only supported screen size.

To support 18:9 and split screen, need to add this to <Application> tag in manifest.

<meta-data android:name="android.max_aspect" android:value="2.1" />