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 691405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:32:23+00:00 2026-05-14T02:32:23+00:00

HI all, I’m just getting started with developing for Android. I’m looking to port

  • 0

HI all,

I’m just getting started with developing for Android. I’m looking to port one of my iPhone applications, but I’m kind of at a loss for how to draw a view at runtime (a view not declared in the XML). Basically, I want to draw a simple rectangle, but then be able to manipulate its frame after being drawn.

Sorry if this is a really, really simple question, but I can’t seem to find some equivalent to the iPhone SDK here.

Thanks in advance!

  • 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-05-14T02:32:24+00:00Added an answer on May 14, 2026 at 2:32 am

    It sounds like you want to experiment with 2D graphics – for that, you should use a Canvas. You can control the drawing of the Canvas through the invalidate() method, which tells Android to redraw the whole thing triggering your customised onDraw() method. You mention not wanting to use the XML file, but that is the simplest way to put in a Canvas – you don’t have to define its contents in the XML file, but simply tell the layout file it’s there. A powerful but simple way to put a Canvas in your application is to customise a View. For example, include in your XML file a <your.package.CustomView android:.../> element. Then declare the CustomView extends View class. Any kind of drawing you want to do, put in the onDraw() method.

    For example, to draw a rectangle, do something like this.

    //First you define a colour for the outline of your rectangle
    rectanglePaint = new Paint();
    rectanglePaint.setARGB(255, 255, 0, 0);
    rectanglePaint.setStrokeWidth(2);
    rectanglePaint.setStyle(Style.STROKE);
    
    //Then create yourself a Rectangle
    Rect rectangle = new Rect(left, top, right, bottom) //in pixels
    
    //And here's a sample onDraw()
    @Override
    public void onDraw(Canvas canvas){
        rectangle.offset(2, 2);
        canvas.drawRect(rectangle, rectanglePaint);
    }
    

    Every time invalidate() is called from your program, the view will be redrawn and the rectangle moved 2px down and to the right. Note: the redrawing only happens with the main thread is ‘waiting’. In other words, if you have a loop calling invalidate several times, the View won’t actually be drawn until the loop finishes. You can get around this, but that adds more complication. For an example of how that’s done, look at the LunarLander example game from Google – it’s a simple game demonstrating a custom View, 2 threads, and how to implement continuous animation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All the articles I've found via google are either obsolete or contradict one another.
All I know about the constraint is it's name ( SYS_C003415 ), but I
All the recent VisualSVN Server posts made me want to check it out. I
All front-end developers know the pain of coding for Firefox, then viewing our then
All I want is to be able to change the color of a bullet
All throughout an application wherever error messages (or other user messages) are used I
All I want is to update an ListViewItem's text whithout seeing any flickering. This
All too often I want a WPF slider that behaves like the System.Windows.Forms.TrackBar of
All of my users are a short walk down the hall, and all of
All, I currently have two projects that are under SourceSafe that I am unable

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.