Android obtains the screen width and height, Status Bar Height, and string width and height of a mobile phone.

Source: Internet
Author: User

Android obtains the screen width and height, Status Bar Height, and string width and height of a mobile phone.

Android obtains the screen width and height, Status Bar Height, and string width and height of a mobile phone.

This article mainly introduces how Android can obtain the screen width and height, Status Bar Height, and string width and height of a mobile phone, this article describes how to obtain text width and height, Status Bar Height, textView width, and screen size for Android. For more information, see

First define the TextView object commentText

Get the width and height of the text:

?

1

2

3

4

5

6

7

8

TextPaint textPaint = new TextPaint (Paint. ANTI_ALIAS_FLAG );

TextPaint. setTextSize (commentText. getTextSize ());

TextPaint. setColor (Color. WHITE );

FontMetrics fontMetrics = textPaint. getFontMetrics ();

Float fTop = fontMetrics. top;

Float fBottom = fontMetrics. bottom;

Float textHeight = (int) (fBottom-fTop );

Float textWidth = (int) textPaint. measureText (commentText. getText ());

Obtain the height of the status bar at the top of the mobile phone screen:

Copy the Code as follows:

DisplayMetrics dm = new DisplayMetrics ();

GetWindowManager (). getDefaultDisplay (). getMetrics (dm );

Int width = dm. widthPixels; // screen width

Int height = dm. heightPixels; // screen height

Rect frame = new Rect ();

GetWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame );

Int statusBarHeight = frame. top; // high status bar

Int contentTop = getWindow (). findViewById (Window. ID_ANDROID_CONTENT). getTop ();

Int titleBarHeight = contentTop-statusBarHeight; // The height of the title bar.

Get the screen width and height of the mobile phone:

Copy the Code as follows:

WindowManager wm = (WindowManager) this. getSystemService (Context. WINDOW_SERVICE );

Int width = wm. getdefadisplay display (). getWidth (); // screen width

Int height = wm. getdefadisplay display (). getHeight (); // screen height

Get textView width

?

1

2

TextPaint paint = textView. getPaint ();

Float len = paint. measureText (string );

Get screen size:

?

1

2

3

4

5

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); // screen size (INCHES)

I hope this article will help you design your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.