SoFunction
Updated on 2025-04-12

How to obtain mobile phone screen width, status bar height and string width and height information

This article describes the method of obtaining the mobile phone screen width and height, status bar height, and string width and height information. Share it for your reference. The details are as follows:

First define the TextView object commentText

Get the width and height of the text:

TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
(());
();
FontMetrics fontMetrics = ();
float fTop = ;
float fBottom = ;
float textHeight = (int)(fBottom - fTop);
float textWidth = (int)(());

Get the status bar height above the phone screen:

Copy the codeThe code is as follows:
DisplayMetrics dm = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(dm); 
int width = ;  //Screen width
int height = ;  //Screen height
Rect frame = new Rect();   
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);   
int statusBarHeight = ;  //Status bar height
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight = contentTop - statusBarHeight; //Title bar height

Get the screen width and height of the phone:

Copy the codeThe code is as follows:
WindowManager wm = (WindowManager) (Context.WINDOW_SERVICE);
int width = ().getWidth();//Screen width
int height = ().getHeight();//Screen height

Get textView width

TextPaint paint = ();
float len = (string);

Get screen size:

DisplayMetrics dm = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(dm); 
double x = (/,2); 
double y = (/,2); 
double screenInches = (x+y); //Screen size(inch)

I hope this article will be helpful to everyone's Android programming design.