Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:47:12+00:00 2026-06-01T04:47:12+00:00

I am trying to programmatically draw a parking icon to place as the drawable

  • 0

I am trying to programmatically draw a parking icon to place as the drawable for an itemized overlay on a map.

The icon consists of a blue square with a white ‘P’ in its centre of which I would like to programmatically change the colour of the square to denote different parking types.

I’ve tried creating it via the canvas using drawRect & drawText but I cannot find a simple way of centering the text in the square and I cannot find a way to center the canvas on the coordinates – it keeps wanting to anchor from the top left hand corner.

I’ve alternatively tried creating an XML layout to convert to a drawable but cannot achieve this either.

Is there an elegant solution for what I am trying to achieve?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:47:14+00:00Added an answer on June 1, 2026 at 4:47 am
    public class TextDrawable extends Drawable {
    
        private final static int    TEXT_PADDING           = 3;
        private final static int    ROUNDED_RECT_RADIUS    = 5;
    
        private final String    text;
        private final Paint     textPaint;
        private final Rect      textBounds;
        private final Paint     bgPaint;
        private final RectF     bgBounds;
    
        public TextDrawable(String text, String backgroundColor, int textHeight) {
    
            this.text = text;
    
            // Text
            this.textPaint = new Paint();
            this.textBounds = new Rect();
            textPaint.setColor(Color.WHITE);
            textPaint.setARGB(255, 255, 255, 255);
            textPaint.setAntiAlias(true);
            textPaint.setSubpixelText(true);
            textPaint.setTextAlign(Paint.Align.CENTER); // Important to centre horizontally in the background RectF
            textPaint.setTextSize(textHeight);
            textPaint.setTypeface(Typeface.DEFAULT_BOLD);
            // Map textPaint to a Rect in order to get its true height
            // ... a bit long-winded I know but unfortunately getTextSize does not seem to give a true height!
            textPaint.getTextBounds(text, 0, text.length(), textBounds);
    
            // Background
            this.bgPaint = new Paint();
            bgPaint.setAntiAlias(true);
            bgPaint.setColor(Color.parseColor(backgroundColor));
            float rectHeight  = TEXT_PADDING * 2 + textHeight;
            float rectWidth   = TEXT_PADDING * 2 + textPaint.measureText(text);
            //float rectWidth   = TEXT_PADDING * 2 + textHeight;  // Square (alternative)
            // Create the background - use negative start x/y coordinates to centre align the icon
            this.bgBounds = new RectF(rectWidth / -2, rectHeight / -2, rectWidth / 2, rectHeight / 2);
        }
    
        @Override
        public void draw(Canvas canvas) {
            canvas.drawRoundRect(bgBounds, ROUNDED_RECT_RADIUS, ROUNDED_RECT_RADIUS, bgPaint);
            // Position the text in the horizontal/vertical centre of the background RectF
            canvas.drawText(text, 0, (textBounds.bottom - textBounds.top)/2, textPaint);
        }
    
        @Override
        public void setAlpha(int alpha) {
            bgPaint.setAlpha(alpha);
            textPaint.setAlpha(alpha);
        }
    
        @Override
        public void setColorFilter(ColorFilter cf) {
            bgPaint.setColorFilter(cf);
            textPaint.setColorFilter(cf);
        }
    
        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to place two uibuttons on uitableviewcell programmatically.. but unable to achieve
I'm trying to adapt an example provided by Apple in order to programmatically draw
Trying to programmatically add options to a SELECT drop down in IE Windows Mobile.
I'm trying to programmatically add an Execute SQL task from within a script task
I am trying to programmatically create a PowerPoint from graphs in Access. Ideally, when
I am trying to programmatically drop a column in a table inside of an
I'm trying to programmatically extract information from an Enterprise Architect model (saved in an
I'm trying to programmatically intercept and cancel a user's attempt to check out a
I'm trying to programmatically add dots to a radar screen. The code runs without
I am trying to programmatically create a number of PDF documents with a watermark

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.