Nexus 7 actual screen size - android

I built a simple application for Nexus 7. I used the following code to get the screen size in DP units.
this.getResources().getConfiguration().screenWidthDp;
this.getResources().getConfiguration().screenHeightDp;
where "this" is MainActivity context object.
I get these values: 600 dp for width and 888 dp for height.
Pixel density is tvdpi which is 213, and the ratio of dp to pixels is 1.33
I used this formula
pixels = dips * (density / 160)
which gives me for height
pixels = 888 * (213 / 160) = 1182.15.
I know that pixel size of the Nexus 7 screen is 800 x 1280. Where are the missing 100 pixels of height in this calculation? Or did I do something wrong?

Configuration.screenHeightDp() returns the dimensions of the available area of the screen.
Your calculated value, 1182, is close to the the height in pixels minus the navigation bar and status bar (1173) of the Nexus 7, in other words the resolution available for your app to use.
Full screen apps should be able to use the full 1280 resolution.

The following should give you the actual display size as a Point:
private Point getDisplaySize(Context context) {
if (Build.VERSION.SDK_INT >= 17) {
return getDisplaySizeMinSdk17(context);
} else if (Build.VERSION.SDK_INT >= 13) {
return getDisplaySizeMinSdk13(context);
} else {
return getDisplaySizeMinSdk1(context);
}
}
#TargetApi(17)
private Point getDisplaySizeMinSdk17(Context context) {
final WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
final Display display = windowManager.getDefaultDisplay();
final DisplayMetrics metrics = new DisplayMetrics();
display.getRealMetrics(metrics);
final Point size = new Point();
size.x = metrics.widthPixels;
size.y = metrics.heightPixels;
return size;
}
#TargetApi(13)
private Point getDisplaySizeMinSdk13(Context context) {
final WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
final Display display = windowManager.getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
return size;
}
#SuppressWarnings("deprecation")
private Point getDisplaySizeMinSdk1(Context context) {
final WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
final Display display = windowManager.getDefaultDisplay();
final Point size = new Point();
size.x = display.getWidth();
size.y = display.getHeight();
return size;
}

Related

Android calculate screen size for draw

I am trying to draw in my app, so I want my brush size be 10% of the device screen which means that for all device it should be diffrent size.
I read in android developer guide Here about dp.
getScreenSizeDpi();
m_brushSize = (int)(area * brush_percentages)/100;
circle_area = m_brushSize * m_brushSize * Math.PI;
}
And the method:
private void getScreenSizeDpi() {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
area = dpHeight * dpWidth;
}
But all I want is to get the circle of the brush size, Thanks all.
Use the following code to get the screen size if you are inside an Activity
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
If you're not inside an Activity, use this instead to obtain the Display object
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Then set the brush size
brush_size = 0.1 * width;// or 0.1 * height
Maybe an update for compose
#Composable
fun ScreenSizedSquare() {
val context = LocalContext.current.resources
val displayMetrics = context.displayMetrics
val screenWidth = (displayMetrics.widthPixels / displayMetrics.density).dp
Box(
modifier = Modifier
.clip(shape = RectangleShape)
.background(Color.Blue)
.width(screenWidth)
.height(screenWidth)
)
}

Get Wrond Display Size . how to convert to original size?

I am getting display size using this function but i get wrong display size in this(moto G2,HTC E8 ,Moto E,Lg G2) device.
but in my other device i got perfect size.
how to I calculate this size?
private void getDefaultDisplay(WindowManager wm) {
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Display display = wm.getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(size);
CAMERA_WIDTH = size.x;
CAMERA_HEIGHT = size.y;
Log.d("Display", CAMERA_WIDTH + ":" + CAMERA_HEIGHT);
} else {
CAMERA_WIDTH = display.getWidth(); // deprecated
CAMERA_HEIGHT = display.getHeight();
}
}
device Name calcualted size : original size
moto G2 720x1184 : 720x1280
HTC E8 1080x1776 : 1080x1920
Moto E 540x888 : 540x960
LG G2 1080x1776 : 1080 x1920
Its easier and nicer to use :
int displayWidth = getResources().getDisplayMetrics().widthPixels;
and
int displayHeight = getResources().getDisplayMetrics().heightPixels;
Try to use this code, i used this in my apps and (in my experience) it always return correct value :
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;

how can i get the "real" resolution of my device?

Yes, I know I can use DisplayMetrics but the problem is that on some devices the widthPixels member is a wrong value (at least i have such one phone). The resolution in setting page is 720*1280, but widthPixels is 600 and heightPixels in DisplayMetrics is 1067.
Additionally, I have tried getRealSize and the returned value is correct. But eventually I found that the "real" width of my phone is 640 (and I guess the height is also a different number).
Awesome! what's really going on here?
Edit:
here're some data
widthPixels in DisplayMetrics is 600. density is 1.6, heightPixels is 1067.
And i have tried all your methods, but all not working. I won't need to post my question here if I could find the correct answer so easily. The device is HN3-U01, made in china
you can use this code..
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Try with this
private DisplayMetrics metrics;
private int widthPixels;
private float scaleFactor;
private float widthDp;
metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density= getResources().getDisplayMetrics().densityDpi;
widthPixels = metrics.widthPixels;
scaleFactor = metrics.density;
widthDp = widthPixels / scaleFactor;
// Get device resolution
int width = 0;
int height = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= 13)
{
w.getDefaultDisplay().getSize(size);
width = size.x;
height = size.y;
}
else
{
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
}
because getWidth() and getHeight() method deprecated after API level 13

Strange behavior of setTextSize (or maybe something else!)

I want to set the text size of my TextView to be proportional to screen width. I'm getting screen width using this code:
public static int getScreenWidth(Context context) {
if (SCREEN_WIDTH == 0) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
SCREEN_WIDTH = outMetrics.widthPixels;
}
return SCREEN_WIDTH;
}
When I set the font size to be a proportion of screen width
textView.setTextSize(screenWidth / something);
I get different results on different devices, particularly Samsung GT-I9000 (android 2.3.3) and Nexus 7 (android 4.3).
However I managed to get it right by doing it this way:
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,
Utils.pixelsToDip(context, screenWidth / something));
It's weird because basically it's like multiplying and dividing by same value.
Here is pixelsToDip method:
public static float pixelsToDip(Context context, float px) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}
Does anyone knows what is the problem here? Maybe there is some faults in pixelToDip method, or I have a misunderstanding of size units?

how to get total screen size on an android device programmatically

if i use the code shown here to get the total screen size it is always short on height to the total screen size as shown in the documented specs for the device. for example I tried to get the screen size by using the code to get the size for a tablet listed as 1280 X 800 and the result from the code is: 1216 X 800. so where is the missing 64 pixels going to?
i can guess that it could be the bar at the bottom of the screen that holds the back and home buttons. but that does not sound right as the content views is supposed to be the root of all views. what is going on here?
the only possible explanation could be this part of the UI
code used to get screen size
// gets the content view windows width and height size
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int widthContentView = displaymetrics.widthPixels;
int heightContentView = displaymetrics.heightPixels;
Toast.makeText(MainActivity.this, "width of content view: " + widthContentView Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "height of content view: " + heightContentView, Toast.LENGTH_SHORT).show();
If you're calling this outside of an Activity, you'll need to pass the context in (or get it through some other call). Then use that to get your display metrics:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
I'm not sure what api you use but if you use api starting from 17 you can use this:
display.getRealSize();
try this code...
Display display = context.getWindowManager()
.getDefaultDisplay();
int width = display.getWidth();
int height=display.getHeight();
it will give screen's width and height,And according to width and height write if- conditions for each device's screen resolution.
You are right, It is showing you the resolution of your app screen and hence you getting the difference.Instead of getMetrics(), use getRealMetrics().
You can use the following code to get the actual screen size of device.
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getRealMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
return width + "*" + height;
int density;
private DisplayMetrics metrics;
private int widthPixels;
private float scaleFactor;
private float widthDp;
metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
density= getResources().getDisplayMetrics().densityDpi;
widthPixels = metrics.widthPixels;
scaleFactor = metrics.density;
widthDp = widthPixels / scaleFactor;
System.out.println("widthDp== "+widthDp );
if(density==DisplayMetrics.DENSITY_HIGH)
System.out.println("Density is high");
if(density==DisplayMetrics.DENSITY_XXHIGH)
System.out.println("Density is xxhigh");
if(density==DisplayMetrics.DENSITY_XXXHIGH)
System.out.println("Density is xxxhigh");
if(density==DisplayMetrics.DENSITY_TV)
System.out.println("Density is Tv");
int Measuredwidth = 0;
int Measuredheight = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
Measuredwidth = size.x;
Measuredheight = size.y;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
Measuredheight = d.getHeight();;
}
After searching for hours through stackOverFlow, couldn't find a single answer that was up to date so thought I would share mine. Hope this helps. :)
Method 1 (Deprecated)
fun Resolution() : String {
var width : Int? = 0
var height : Int? = 0
try {
val displayMetrics = DisplayMetrics()
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.defaultDisplay.getRealMetrics(displayMetrics)
width = displayMetrics.widthPixels
height = displayMetrics.heightPixels
}catch (e : Exception){
e.printStackTrace()
Log.e("DisplayClass", e.message, e)
}
return "$width x $height pixels"
}
Difference between getRealMetrics(displayMetrics) and getMetrics(displayMetrics) is that getMetrics() will return height/width without adding the size of status bars and navigation bars
windowManager.defaultDisplay.getRealMetrics(displayMetrics) // e.g: 1080 x 2340
windowManager.defaultDisplay.getMetrics(displayMetrics) // e.g: 1080 x 2260
Method 2 (Deprecated) (Minimum API : 30 (R))
This method returns the same result as getMetrics()
#RequiresApi(Build.VERSION_CODES.R)
fun Resolution() : String {
val width = context.display?.width
val height = context.display?.height
return "$width x $height"
}
Method 3 (Working as of 24th August 2022)
fun Resolution() : String {
var width: Int? = 0
var height : Int? = 0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val metrics = windowManager.currentWindowMetrics
width = metrics.bounds.width()
height = metrics.bounds.height()
}else{
val displayMetrics = DisplayMetrics()
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.defaultDisplay.getRealMetrics(displayMetrics)
width = displayMetrics.widthPixels
height = displayMetrics.heightPixels
}
return "$width x $height"
}
Get all Display set your width and height as you required.
Display display;
display=getWindowManager().getDefaultDisplay();
text1.setWidth(display.getWidth()-380);
text1.setHeight(display.getHeight()-720);

Categories

Resources