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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:47:15+00:00 2026-06-02T07:47:15+00:00

I am trying to implement a custom view inside of a HorizontalScrollView parent. The

  • 0

I am trying to implement a custom view inside of a HorizontalScrollView parent. The custom view repeatedly draws a line to its Canvas based on timing from a Handler. Right now the custom view fills the width of the screen (limited by 548dp height) and I want the line to be able to extend beyond what is visible. Furthermore, I want to allow the user to then be able to horizontally scroll to see more of the line. I tried to set the width of the HorizontalScrollView to 1500dp (which is much larger than the width of the screen) and then I tried to scroll horizontal, but it didn’t move. It would also be ideal if the view would scroll with the line as it grew past the visible part of the screen.

I am developing on an ASUS Transformer with Honeycomb.

Here’s the relevant part of the layout I’m using:

<HorizontalScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="1500dp"
    android:layout_height="548dp"
    android:fillViewport="true" >

    <maavapp.layout.CustomDraw
        android:id="@+id/custom_draw"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </maavapp.layout.CustomDraw>
</HorizontalScrollView>

And here is the code for the CustomDraw class:

public class CustomDraw extends View {
private static int mSelected;
private ArrayList<Coordinate> measure1;
private ArrayList<Coordinate> measure2;
private ArrayList<Coordinate> measure3;
private boolean north = false, east = true, south = true, west = false;
private DrawHandler dh = new DrawHandler();
private boolean draw = true;
private int width;
private int height;
private int m2;

public CustomDraw(Context context) {
    super(context);
    init();

}

public CustomDraw(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CustomDraw(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public void init() {        
    measure1 = new ArrayList<Coordinate>();
    measure2 = new ArrayList<Coordinate>();
    measure3 = new ArrayList<Coordinate>();

    mSelected = Constants.MEASURE_1;
    width = 0;
    height = 0;
    m2 = 0;
}

public void setMeasure(int measure) {
    mSelected = measure;
}

public void toggleDraw() {
    draw = !draw;
    if(draw) {
        updateLine();
    }
}

public boolean isDrawing() {
    return draw;
}

public void updateLine() {
    // grab new coordinates for each measure
    /*new_coord(measure1);
    new_coord(measure2);
    new_coord(measure3);*/

    if(measure1.isEmpty() && measure2.isEmpty() && measure3.isEmpty()) {
        measure1.add(new Coordinate(0, 0));
        measure2.add(new Coordinate(0, 0));
        measure3.add(new Coordinate(0, 0));
    } else {
        Coordinate last_coord = measure1.get(measure1.size() - 1);

        measure2.add(new Coordinate(++m2, 25));

        /*if(last_coord.x >= width) {
            east = false;
            west = true;
        } else if(last_coord.x <= 0) {
            east = true;
            west = false;
        }*/

        if(last_coord.y >= height) {
            south = false;
            north = true;
        } else if(last_coord.y <= 0) {
            south = true;
            north = false;
        }

        Log.d("MAAV", "last_coord.x + 3: " + (last_coord.x + 3));
        Log.d("MAAV", "last_coord.y + 3: " + (last_coord.y + 3));

        if(south && east) {
            measure1.add(new Coordinate(last_coord.x + 3, last_coord.y + 3));
        } else if(south && west) {
            measure1.add(new Coordinate(last_coord.x - 3, last_coord.y + 3));
        } else if(north && east) {
            measure1.add(new Coordinate(last_coord.x + 3, last_coord.y - 3));
        } else if(north && west) {
            measure1.add(new Coordinate(last_coord.x - 3, last_coord.y - 3));
        }
    }

    if(draw) {
        dh.sleep(10);
    }
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    this.width = w;
    this.height = h;
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
public void onDraw(Canvas c) {
    super.onDraw(c);
    Paint p = new Paint();
    p.setStyle(Paint.Style.FILL);

    p.setColor(Color.WHITE);
    c.drawPaint(p);
    p.setColor(Color.BLACK);

    switch(mSelected) {
    case Constants.MEASURE_1:
        for(int i = 0; i < measure1.size(); i++) {
            Coordinate coord = measure1.get(i);
            Log.d("MAAV", "drawing coord.x, coord.y: " + (coord.x) + ", " + (coord.y));
            c.drawRect(coord.x, coord.y, coord.x + 3, coord.y + 3, p);  
        }
        break;
    case Constants.MEASURE_2:
        for(int i = 0; i < measure2.size(); i++) {
            Coordinate coord = measure2.get(i);
            c.drawRect(coord.x, coord.y, coord.x + 3, coord.y + 3, p);
        }
        break;
    case Constants.MEASURE_3:
        for(int i = 0; i < measure2.size(); i++) {
            Coordinate coord = measure2.get(i);
            c.drawRect(coord.x, coord.y, coord.x + 3, coord.y + 3, p);
        }
        break;
    }

}

class DrawHandler extends Handler {

    @Override
    public void handleMessage(Message msg) {
        CustomDraw.this.updateLine();
        CustomDraw.this.invalidate();
    }

    public void sleep(long delayMillis) {
        this.removeMessages(0);
        sendMessageDelayed(obtainMessage(0), delayMillis);
    }
}
}

Thanks for any help!

  • 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-02T07:47:16+00:00Added an answer on June 2, 2026 at 7:47 am

    For anyone that’s curious, I figured out the answer for my situation. Hopefully what I outline below could be helpful to you too.

    In order to get the custom View to scroll, I had to add a LinearLayout wrapper around my custom View. Not 100% sure why.

    Also, though I declare a fixed size for my custom View (in the xml file below), I’m able to update it dynamically in my code using:

    CustomDraw.this.setLayoutParams(new LinearLayout.LayoutParams(width + {adjust size}, height));
    

    where “width” and “height” are the width and height of the custom View. Again, not sure why I have to say “new LinearLayout.LayoutParams(…);” but that’s what seems to work.

    Here’s the xml I’m using:

    <HorizontalScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scroller"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fillViewport="true" >
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="548dp"
            android:orientation="vertical" >
    
            <maavapp.layout.CustomDraw
                android:id="@+id/custom_draw"
                android:layout_width="4800dp"
                android:layout_height="fill_parent" >
            </maavapp.layout.CustomDraw>
        </LinearLayout>
    </HorizontalScrollView>
    

    And here’s the relevant parts of the custom class code I’m using:

    public class CustomDraw extends View {
    // member variables
    
    public void setHSV(HorizontalScrollView hsv) {
        this.hsv = hsv;
    }
    
    public void updateLine() {
        if(measure1.isEmpty()) {
            measure1.add(new Coordinate(0, 0));
        } else {
            Coordinate last_coord = measure1.get(measure1.size() - 1);
    
                        // calculate south, north, east, and west
    
            if(((last_coord.x + 3) % 1200 == 0) 
                    || ((last_coord.x + 3) % 1200 == 1) 
                    || ((last_coord.x + 3) % 1200 == 2)
                    || ((last_coord.x + 3) % 1200 == 3) && (last_coord.x + 3 >= 1200)) {
                hsv.smoothScrollTo(last_coord.x - 50, 0);
            }
            if(last_coord.x >= (width - 100)) {
                this.setLayoutParams(new LinearLayout.LayoutParams(width + 4800, height));
                width += 4800;
            }
    
            if(south && east) {
                measure1.add(new Coordinate(last_coord.x + 3, last_coord.y + 3));
            } else if(south && west) {
                measure1.add(new Coordinate(last_coord.x - 3, last_coord.y + 3));
            } else if(north && east) {
                measure1.add(new Coordinate(last_coord.x + 3, last_coord.y - 3));
            } else if(north && west) {
                measure1.add(new Coordinate(last_coord.x - 3, last_coord.y - 3));
            }
        }
    
        if(draw) {
            dh.sleep(10);
        }
    }
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        this.width = w;
        this.height = h;
        super.onSizeChanged(w, h, oldw, oldh);
    }
    
    @Override
    public void onDraw(Canvas c) {
        super.onDraw(c);
        Paint p = new Paint();
        p.setStyle(Paint.Style.FILL);
    
        p.setColor(Color.WHITE);
        c.drawPaint(p);
        p.setColor(Color.BLACK);
    
            Coordinate coord;
        for(int i = 0; i < measure1.size(); i++) {
            coord = measure1.get(i);
            c.drawRect(coord.x, coord.y, coord.x + 3, coord.y + 3, p);  
        }
    }
    
    class DrawHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            CustomDraw.this.updateLine();
            CustomDraw.this.invalidate();
        }
    
        public void sleep(long delayMillis) {
            this.removeMessages(0);
            sendMessageDelayed(obtainMessage(0), delayMillis);
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a custom view inside of a HorizontalScrollView parent. The
I'm trying to implement a view controller for a custom NSOpenGLView based view (this
I am trying to implement a custom view, for which I want to be
I'm trying to implement to open a custom dialog box having related info from
I'm trying to implement a custom MapView. Inside my MapActivity (named mainmap) I have
im trying to implement a custom error page, what i want to be able
i'm trying to implement Woopra custom event data on page load, and i'm using
I am trying to implement a custom broken image icon to appear if I
I'm trying to implement a custom control in C# and I need to get
I'm trying to implement a custom form builder, similar to those provided by Wufoo

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.