[SOLVED] Wrong screen width on certain Samsung devices


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

Issue

4 users using Samsung devices A52 and Tab10.1 reported that the app’s playing area not filling entire screen as shown below src=”https://i.stack.imgur.com/eEUoB.png” alt=”screenshow on Samsung A52″ />

According to GSMarena this model screen width is 1080 pixels, all the views is set to Match_Parent in xml , and getting width of any view while app running returns 810 pixel not 1080 however the views as shown fill enter screen

Below methods which i use to get screen width also all return 810 not 1080

Method 1

DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.widthPixels;

Method 2

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

Method 3

return activity.getResources().getDisplayMetrics().widthPixels;

And as shown in the screenshot the bitmap created according to screen width 810 and not filling all screen

The only method which returned correct screen width is

WindowMetrics windowMetrics = activity.getWindowManager().getCurrentWindowMetrics();
Insets insets = windowMetrics.getWindowInsets().getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
return windowMetrics.getBounds().width() - insets.left - insets.right;

But after updating the game with this method , other users reported that the bitmap is now drawn in a larger area than the screen , which mean this method return 125% of their actual screen width , their devices are Samsung M62 & Samsung Note10 Lite

My question is : is their any screen scale factor that users can mess with in accessibility or something and we have to consider while getting screen width? or what causing this problem

Note that i tried the game on another A52 device and game worked correctly

Solution

after 6 months of searching and trying to find why bitmap displays sometimes larger and other times smaller than ImageView in certain models however both created programmatically and using same width/height pixels I finally found a solution that worked on all the exceptions mentioned above

bitmap.setDensity(Bitmap.DENSITY_NONE);

Answered By – TripleTroop

people found this article helpful. What about you?