Igor Evdokimov
Igor Evdokimov

Reputation: 105

Selecting layout parameters for a concrete model of smartphone

I'm a newbie in mobile development and I try to develop an Android application for several models of smartphones and tablets.
Now I'm experiencing some problems with selecting an appropriate layout parameters for Xiaomi Redmi 6a [https://www.gadgetsnow.com/mobile-phones/Xiaomi-Redmi-6A]
I designed a layout specially for it : land-xhdpi-1440x720 But when I try to run my app it seems to be, that it selects land-xhdpi-800x480.

Why so? What Am I doing wrong in this case?

And, by the way, could you recommend me some articles about layout selection for different types of devices based on some real experience (not an Android Developers Manual)?

Upvotes: 0

Views: 41

Answers (2)

Crispert
Crispert

Reputation: 1167

According to the layout folder spec it does not appear to allow specifying both screen dimensions in a layout folder name.

If you want to check the device model in your app you can use the Build class (the BRAND, MODEL and DEVICE fields) and select the appropriate layout at runtime by calling setContentView(R.layout.*) in your Activity/Fragment. However, you should be aware that matching the device model to the screen resolution can be challenging, especially since on some phones the resolution can be changed in settings. It would be safer to check the actual resolution at runtime:

DisplayManager mgr = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE)
Display[] allDisplays = mgr.getDisplays();
Display targetDisplay;
if (allDisplays != null && allDisplays.length > 0) {
    //select the display to use, usually there is only one (or find the one with the highest resolution)
    targetDisplay = allDisplays[0];
    //get the resolution
      Point sizePt = new Point();
      targetDisplay.getRealSize(sizePt);
    //or
      DisplayMetrics metrics = new DisplayMetrics();
      targetDisplay.getRealMetrics(metrics);

}

Also note the actual values may not be equal to the physical resolution due to the height of the notification and navigation bars so be sure to run some tests and try to use more general comparisons than

sizePt.x == 1440 && sizePt.y == 720

Upvotes: 1

Sergey Glotov
Sergey Glotov

Reputation: 20346

  1. It's considered bad practice. Why do you need layout for particular device?
  2. Screen dimension for resource qualifier is set in dp, not in pixels. For xhdpi screen with 1440x720 px, screen size will be 720x360 dp

Upvotes: 1

Related Questions