0

I am trying to differentiate layouts in Android (ICS+) for larger devices like the note 3 but finding it next to impossible to do without reading the device name or attempting to calculate physical size in code.

The (deprecated) size attribute for the note 3 identifies as normal, as does the HTC One Mini.

Same goes for using sw350dp / xxhdpi. Not sure if the HTC Mini is the unusual device here, but I would imagine all the newer high-end mini handsets will be the same. I also don't want to use px values if I can help it as smaller devices can (and probably will) go up to 1080p, and notes prior to 3 use lower.

Is there something I am missing here? If not, is any reliable way to calculate the physical size or am I better off hard-coding device lists to provide optimal layouts?

EDIT

So part of this was due to multiple overriding layout folders causing some funkiness and I now have the Mini using the correct values, however I am still in the same situation with the S4 and Note 3 using the same resources. While this is not as bad, it still isn't optimal.

I expect that the Note 1 and 2 will also drop back to the lower DPI resources meaning their real-estate won't be utilised either.

Ben Neill
  • 2,433
  • 1
  • 20
  • 20
  • `sw350dp` is measured in density-independent pixels. "smaller devices can (and probably will) go up to 1080p" -- but they will remain the same size in density-independent pixels. Use `-swNNNdp`, `-wNNNdp`, and `-hNNNdp` for your phablet scenarios. – CommonsWare Nov 21 '14 at 02:46
  • @CommonsWare Yes but I don't see how I will differentiate layouts between physically small and large - things like touch areas become a problem otherwise. – Ben Neill Nov 21 '14 at 02:48
  • "Physically small" = lower number of density-independent pixels. "Physically large" = higher number of density-independent pixels. Choose values that are where you want to change UIs, just as Web designers choose `dp` values for CSS media queries for deciding when to apply certain CSS rules. http://developer.android.com/guide/practices/screens_support.html#range – CommonsWare Nov 21 '14 at 02:52
  • @CommonsWare Essentially I am trying to decide whether to show 3 or 4 columns in a gridview. So in the case of the HTC Mini and the SGS Note 3, they have the same sw dp size and both are xxhdpi - I am not sure how that will help... I *can* show the same layouts on both devices but text and touch areas become too small. – Ben Neill Nov 21 '14 at 02:57
  • "HTC Mini and the SGS Note 3, they have the same sw dp size" -- then they are the same physical size. `1dp` = 1/160th of an inch, for all devices. If they are not the same physical size, either you are doing your calculations wrong, or one of the manufacturers screwed up something with the reported density. – CommonsWare Nov 21 '14 at 23:13
  • @CommonsWare That may be the issue: The HTC Mini is 341ppi, 4.3" while the note 3 is 386ppi 5.7". From what I understand that should put them both in the xhdpi bucket (being under 440ppi). The HTC M7 (which the mini is based on) is 469ppi putting it in the xxhdpi bucket - they have probably decided it looks good with the higher bucket and left as-is for maintainability. The note 3 may be the same story. – Ben Neill Nov 24 '14 at 00:33

2 Answers2

1

Copy and paste this code into your Activity and when it is executed it will Toast the device's screen size category.

int screenSize = getResources().getConfiguration().screenLayout &
        Configuration.SCREENLAYOUT_SIZE_MASK;

String toastMsg;
switch(screenSize) {
    case Configuration.SCREENLAYOUT_SIZE_LARGE:
        toastMsg = "Large screen";
        break;
    case Configuration.SCREENLAYOUT_SIZE_NORMAL:
        toastMsg = "Normal screen";
        break;
    case Configuration.SCREENLAYOUT_SIZE_SMALL:
        toastMsg = "Small screen";
        break;
    default:
        toastMsg = "Screen size is neither large, normal or small";
}
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();

Or on the other hand you can get the screen size in inches and categorize it as your requirement

 DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(dm.widthPixels/dm.xdpi,2);
    double y = Math.pow(dm.heightPixels/dm.ydpi,2);
    double screenInches = Math.sqrt(x+y);
    Log.d("debug","Screen inches : " + screenInches);

screenInches=  (double)Math.round(screenInches * 10) / 10;

How i handled this in my project...
Made different Activities,
And set content view accordingly.
I wish you find a better solution, but what it looks like is, this is the only way.

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
        double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
        screenInches = Math.sqrt(x + y);
        Log.i("Screen inches : ", "" + screenInches);
        removeGarbage();
        if(screenInches<=6){
            setContentView(R.layout.activity_management);
        }
        else {
            setContentView(R.layout.activity_tab_managment);
        }

        // Do whatever you want
}
Salmaan
  • 3,648
  • 8
  • 33
  • 59
  • Screen size is deprecated, and as I said, the Note 3 identifies as 'normal', as does the HTC One Mini. – Ben Neill Nov 21 '14 at 06:01
  • As for the second part of this, is that reliable? I have read about poeple having issues with similar methods... – Ben Neill Nov 21 '14 at 06:02
  • For the second part you should definitely try as it works fine for me, i tried it on SamsungS3, HTC M8, Xperia Z, Nexus 7 (Tab) and Samsung Galaxy Tab 4... As you need a rough idea it will work fine. – Salmaan Nov 21 '14 at 06:13
  • I will have a look at it monday and let you know how I go. Was hoping for something cleaner but it is looking like this is the only way currently. – Ben Neill Nov 21 '14 at 06:55
  • The second half does respond as expected but it is a massive hack really - I would have to handle a heap of styling in order for this to work. I am hoping to find a better method of handling this. – Ben Neill Nov 24 '14 at 01:05
0

Do NOT use screen size approach. Some phablet users who have Galaxy Mega 5.8/6.3 device or Nexus 6 may have unintended experience.

Use resource based approach sw600dp and sw720dp as below link.

Tablet or Phone - Android

Community
  • 1
  • 1
Youngjae
  • 22,962
  • 17
  • 106
  • 189