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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:46:06+00:00 2026-05-20T04:46:06+00:00

I am trying to work through an android example given on the developers’ page.

  • 0

I am trying to work through an android example given on the developers’ page.
It gives 2 ways of drawing on a canvas. The first way is to use a class called CustomDrawableView, which looks like this:

public class CustomDrawableView extends View {
private ShapeDrawable mDrawable;

public CustomDrawableView(Context context) {
super(context);

int x = 10;
int y = 10;
int width = 300;
int height = 50;

mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}

protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
}

This is then called by the main class:

CustomDrawableView mCustomDrawableView;
protected void onCreate(Bundle savedInstanceState) {    
super.onCreate(savedInstanceState);    
mCustomDrawableView = new CustomDrawableView(this);        
setContentView(mCustomDrawableView);
}

But then the tutorial says:

If you’d like to draw this custom drawable from the XML layout instead of from the Activity, then the CustomDrawable class must override the View(Context, AttributeSet) constructor, which is called when instantiating a View via inflation from XML. Then add a CustomDrawable element to the XML, like so:

<com.example.shapedrawable.CustomDrawableView
   android:layout_width="fill_parent"     
android:layout_height="wrap_content"     
/>

I have successfully got the first example to run but I do not know what my code should look like if I want to adopt the second option.
Presumably, I start with @Override View but then I am stuck.

The reason I want to do this is because I want to create a canvas that covers only part of the screen so I can fit buttons and text in the other part.

  • 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-20T04:46:07+00:00Added an answer on May 20, 2026 at 4:46 am

    If the tutorial is correct then the only change you make is to add that constructor. You probably want to move the drawable init code to a separate method.

    public CustomDrawableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initDrawable();
    }
    

    The xml

    <com.example.shapedrawable.CustomDrawableView
       android:id="+id/custom_drawable_view"
       android:layout_width="fill_parent"     
        android:layout_height="wrap_content"     
    />
    

    And you can obtain the view like this.

    CustomDrawableView mCustomDrawableView;
    
    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);          
        setContentView(R.layout.main); // assuming your layout is named main.xml
        mCustomDrawableView = (CustomDrawableView) findViewById(R.id.custom_drawable_view);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to work my way through Project Euler, and have noticed
I'm trying to work my way through Ron Jeffries's Extreme Programming Adventures in C#.
I,m trying to work through and test a Voice Recognition example based on the
I am trying to work my way through the XNA MSDN documentation on saving
I was trying to work my way through the nodepad tutorial on Dailyjs.com (found
I'm trying to work through Project Euler and I'm hitting a barrier on problem
I'm trying to work through the problems on projecteuler.net but I keep running into
I'm trying to work through this guide to Rails routing , but I got
I'm trying to work out a way of passing the web current http context
I'm going through an Android tutorial and I'm trying to access a color I've

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.