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

Ask A Question

Stats

  • Questions 347k
  • Answers 347k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I usually cheat and allow the ViewModel access to the… May 14, 2026 at 6:26 am
  • Editorial Team
    Editorial Team added an answer The better is to determine if there are any chars… May 14, 2026 at 6:26 am
  • Editorial Team
    Editorial Team added an answer Put it anywhere you like, maybe make a new directory… May 14, 2026 at 6:26 am

Related Questions

HI All, I have a piece of javaScript that removes commas from a provided
Hi All, I'm designing a user control, briefly it contains an asp:hiddenfield control, i'm
Hi All, It's clear wehn I change any control and then posts back the
HI all, I use jQuery to parse my xml responses. I have this xml
Hi All, I'm thinking in this line of code IDataReader myReader = questDatabase.ExecuteReader(getQuest); I'm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.