Android gets mobile screen width, status bar height and string width information method

Source: Internet
Author: User
Tags pow

Android gets mobile screen width, status bar height and string width information method

This article mainly introduces Android to get the phone screen width, status bar height and string width high information method, involving Android to get text width high, status bar height, textview width and screen size related skills, need friends can refer to the

First define the TextView object Commenttext

Get the width 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 ());

Get the status bar height above the phone screen:

Copy code code as follows:

Displaymetrics dm = new Displaymetrics ();

Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);

int width = dm.widthpixels; Screen width

int height = dm.heightpixels; High screen

Rect frame = new Rect ();

GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);

int statusbarheight = Frame.top; Status bar Height

int contenttop = GetWindow (). Findviewbyid (window.id_android_content). GetTop ();

int titlebarheight = Contenttop-statusbarheight; High title bar

Get Phone screen width:

Copy code code as follows:

WindowManager wm = (WindowManager) this.getsystemservice (Context.window_service);

int width = Wm.getdefaultdisplay (). getwidth ();//Screen width

int height = Wm.getdefaultdisplay (). GetHeight ();//Screen height

Get TextView width

?

1 2 Textpaint paint = Textview.getpaint (); float len = Paint.measuretext (string);

To get the 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 with 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.