Getting height and width of a text using Canvas

I’m developing an Android 2.2 application.

I’m using this method to draw a text in a View:

  • Android MediaCodec not decoding all input buffers
  • How to load AnimationDrawable from xml file
  • How would I implement a swipe-based circular control like this?
  • wrap a layout with textviews
  • Android: TabHost - passing Parameters to Fragments
  • Use parcelable to store item as sharedpreferences?
  •     public void draw(Canvas c)
            if(name != null)
                c.drawText(name, getLeft(), getTop(), p);

    How can I get height and width of name text?

    If I do this (p is a Paint object):

    p.getTextBounds(name, 0, name.length(), bounds);

    I get With name = 'Loading', bounds = Rect(1, -10 - 42, 3);.

    I don’t know why I get this strange rectangle.

    Any clue?

    This is my possible solution:

    public class MyView extends ARSphericalView
        public String name;
        public MyView(Context ctx)
            inclination = 0;
        public void draw(Canvas c)
            if(name != null)
                c.drawText(name, getLeft(), getTop(), p);
            if(name != null)
                Rect bounds = new Rect();
                c.drawText(name, getLeft(), getTop(), p);
                p.getTextBounds(name, 0, name.length(), bounds);
                c.drawRect(bounds, p);

    But, it doesn’t work. I get that strange rectangle.

    Related posts:

    Does proguard work to obfuscate static string constants?
    FrameLayout to RelativeLayout ClassCastException even if there is no FrameLayout used
    JUnit testing for Android app with fragments
    Fragment lifecycle - which method is called upon show / hide?
    Android toggle button custom look
    How to change background color popup menu android
  • Update android SDK : install latest platform to implement new APIs such as “ShortcutManager”
  • How to get the device UUID in ionic framework
  • What is AttributeSet and how can i use it?
  • Play an audio clip onto an ongoing call
  • Removing Log call using proguard
  • `/storage/emulated/legacy/` vs `/storage/emulated/0/` vs `data/data/myApp'
  • 2 Solutions collect form web for “Getting height and width of a text using Canvas”

    A text size is measured from its baseline, and has an ascent (upwards, so -y) and descent (downwards, y). The first y-value in your rect (-10) is the ascent, the second the descent (3). Width of the text is 41 (42-1). Height thus is |ascent| + |descent| is 10 + 3 = 13;

    Similarly p.getFontMetrics() has top and bottom attributes, which describe the highest ascent and descent the font you are using have. If you want to calculate the height of a text, then its Math.abs(p.ascent) + Math.abs(p.descent) You can also measure the width of a text with p.measureText().

    You can use Paint.setTextSize() to set the text size, and then paint it in the Canvas.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.