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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:38:24+00:00 2026-06-04T07:38:24+00:00

I have a canvas, in which I want to paint several rectangles, I can

  • 0

I have a canvas, in which I want to paint several rectangles, I can paint with canvas in an InternalView, but I need to paint in a scrollview, I tried with this code, but some have evil.
This is the code:

   package prueba.android.ondraw;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class Prueba_ondrawActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
        setContentView(R.layout.main);

        MyView v = new MyView(this);
        layout.addView(v);

    }
    private class MyView extends View{
        public MyView(Context context) {
            super(context);
        }
        protected void onDraw(Canvas canvas) {
            int j = 0;
            for(int i=0;i<10;i++){
                super.onDraw(canvas);
                Paint paint = new Paint();
                paint.setColor(Color.CYAN);
                paint.setAntiAlias(true);
                canvas.drawRect(0, j,50,50, paint);
                j= j+100;
            }
        }
    }
}

Main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@android:color/darker_gray">

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="150dp"
        android:layout_height="400dp"
        android:layout_marginLeft="100dp" >
        <LinearLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </LinearLayout>
    </ScrollView>

</LinearLayout>

Help please, thank you!

  • 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-04T07:38:25+00:00Added an answer on June 4, 2026 at 7:38 am

    You are doing some strange things in your code:
    first you try to find a view (findViewById()) before you have used setContentView() (sv will always be null here, because there are no views yet), then you replace that View with a new ScrollView (which you don’t use), and finally you set the content to a layout.

    Instead what you probably want to do is create your own View, and then use that inside a ScrollView?

    In your ScrollView, you have a layout, this will need an ID:

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="150dp"
        android:layout_height="400dp"
        android:layout_marginLeft="100dp" >
        <LinearLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </ScrollView>
    

    Then you find this layout in code:

    setContentView(R.layout.main);
    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
    //Create a new instance of MyView...
    MyView v = new MyView(this);
    //...And add it to the layout:
    layout.addView(v);
    

    And you can define your own MyView like this:

    private class MyView extends View{
        //Your onDraw() method here.
    }
    

    (Notice that I call the custom View MyView. This is because android already have a class named View, so it could cause lots of confusion if you give classes names that already exist)

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

Sidebar

Related Questions

I have a few internal Canvas xaml files which I want to load dynamically
I have a canvas which listens for mouse down event... but I wanted to
I have a Canvas that contains a button which I want to be able
i have some coordinates, which i want to draw on a HTML5 canvas step-by-step.
I have a canvas which contains specific usercontrols. At some point, I want to
Basically I have several canvas drawings on my page what I want to happen
I have a canvas which contains a single myComponentA. A myComponentA contains a MyComponentB.
I have a Canvas which has many components inside it and those again, have
On my application, I have a canvas which CSS size (CSS, not html) updates
In my WPF application I have a Canvas in which I do some drawing.

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.