3

In our application, we need to determine screen width at startup. We have tried to get this width using a few different methods (see list below), but Samsung devices, especially their Galaxy devices, are sometimes giving us incorrect values.

We have a Galaxy s6 with a resolution of 1440 x 2560 pixels. Most of the time the device will report a width of 1440, but occasionally it will give us a width of 1080.

We have tried getting the width the following ways:

1)

getResources().getDisplayMetrics().widthPixels;

2)

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);

3)

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getRealSize(size);

But in all cases we see an incorrect width being returned. We see incorrect values reliably when our application is launched for the very first time. Any help would be hugely appreciated.

Jesse Buss
  • 833
  • 6
  • 13
  • Well if you know the size in advance for certain models, why do you need to query it and not have some sort of lookup table or something? – PmanAce Oct 25 '16 at 18:25
  • Yes, we could do that but maintaining that massive list is not ideal. – Jesse Buss Oct 25 '16 at 18:29
  • Yes I know, I was talking about just the few problematic cases like this one. But if someone finds a solution, use it. :) – PmanAce Oct 25 '16 at 18:31
  • Are you sure it's not an error in debugging rather than an error in the app? the docs suggest you can get smaller screen sizes when "the window manager is emulating a smaller display " https://developer.android.com/reference/android/view/Display.html#getRealMetrics(android.util.DisplayMetrics) – Dave S Oct 25 '16 at 18:34
  • Yes I'm sure. We grab the screen width from the device in production and send it to an endpoint. The endpoint shows us sending an incorrect width some of the time. – Jesse Buss Oct 25 '16 at 18:36
  • 1
    When I test my app, I sometimes see that my S6 Edge changes the screen size and density to fit the app – Zoe Oct 28 '16 at 15:58

3 Answers3

0

Have you tried this?

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = metrics.widthPixels;

UPDATE

And a little bit hardcore. You may find more here.

Community
  • 1
  • 1
Artem Stepanenko
  • 3,423
  • 6
  • 29
  • 51
0

This is our getDeviceWidth() method for ages and we never had any trouble with it, but i guess it's your second version?! Please verify and comment. (Ensure you call this after your views are created.)

public static int getDeviceWidth(Activity activity) {

    Display display = activity.getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    return size.x;
}

Otherwise i would try to get the y-position (or width) of an dummy layout which has sets its width to match_parent.

And test your getWidth on start and after app is running (f.e. on button click). Does it return wrong width during run time too?

longi
  • 11,104
  • 10
  • 55
  • 89
  • Yes we have used this method with no success. We are calling this method in our Application's onCreate(). – Jesse Buss Oct 28 '16 at 15:09
  • isn't that too early? because views are not rendered yet. this happens in `onCreateView`. that's why i recommend calling your method during runtime, to ensure that this is not the issue – longi Oct 28 '16 at 15:13
0

In my method, I haven't run into any issues before in terms of not locating the right location of something or the exact size of any Android screen.

My method finds the screen size in inches and the screen height and width in pixels and inches

In my app this method is how I check if my app is being used by a tablet:

private void tabletChanges() {
    DisplayMetrics dm = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    int dens = dm.densityDpi;
    double wi = (double)width/dm.xdpi;
    double hi = (double)height/dm.ydpi;
    double x = Math.pow(wi,2);
    double y = Math.pow(hi,2);
    double screenInches = Math.sqrt(x+y);

    if(screenInches >= 7){
        //Do certain things
    }
}
Lazar Kukolj
  • 696
  • 3
  • 15
  • 43