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

  • SEARCH
  • Home
  • 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 6663075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:27:35+00:00 2026-05-26T02:27:35+00:00

I am trying to add a canvas view to a relative layout inside a

  • 0

I am trying to add a canvas view to a relative layout inside a horizontal scroll view.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#fff"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/hsv"
        android:layout_width="fill_parent"
        android:background="#EEDB00"
        android:layout_height="30mm">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/canvas"
            android:background="#000"
            android:layout_width="300mm"
            android:layout_height="20mm">
        </RelativeLayout>

    </HorizontalScrollView>
</RelativeLayout>

I created a class called CanvasView which extends View, and I drew some basic shapes by over-riding onDraw(). However the canvas does not appear in the relativelayout, when I do:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        cView = new CanvasView(this);
        rLayout = (RelativeLayout)this.findViewById(R.id.canvas);
        rLayout.addView(cView);
    }

However, when I directly add it by calling setContentView(cView); it works. On digging, I found that when I call addView(), the onDraw() is not firing at all, and hence the canvas is not drawn… Any ideas on how to fix this?

  • 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-26T02:27:35+00:00Added an answer on May 26, 2026 at 2:27 am

    I’m not in a position to test this myself at the moment, but I think your problem is that you’re not applying any LayoutParams to the View, which may mean it occupies no screen space. If a View is ever off-screen or completely obscured or the system otherwise decides that nothing it draws will be visible, then I believe onDraw() won’t be called at all.

    Try setting some width and height to your View when you add it to your RelativeLayout:

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50,50);
    rLayout.addView(cView, params);    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to add gridlines to a canvas. I need an exact free
i'm trying to add some animated gif into static image (canvas), here's my start
I am trying to use Javascript in order to add a canvas to one
I am trying to add multiple texts on canvas element of HTML5 using context.fillText(...)
I'm just trying to add a double click event to a HTML5 Canvas element.
I'm trying to add a custom ImageView to my main.xml , but if I
I'm trying to add a horizontal line to my axes to mark an important
I have LinearLayout in my xml file that I am trying to add a
I know how to add a control to the canvas/grid/layout- simply by calling canvas.Childern.Add()
I'm trying add a tab to my web page that looks like this: Using

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.