1

I've created a custom view which creates a bitmap and then in the onDraw() method I display an objects data.

Also in the XML file I have made the view horizontally scrollable.

My question is after I've drawn all the data on the canvas from the onDraw() method, how can I increase the view's size to show all the data.

i.e I want the view to scroll just enough until the data has finished and not any more.

I do have the y-coordinate for the last item displayed.

Currently I'm setting the width of the view to 800dp in the XML file, but for some object's it cut's off some of the data.

Thanks

user557240
  • 273
  • 5
  • 15
  • I've also looked at setting the layout parameters in the constructor should I do this in the onDraw() method? – user557240 Oct 20 '12 at 14:05

1 Answers1

3

You can override the onMeasure() method to adjust the size of your View:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int parentViewWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentViewHeight = MeasureSpec.getSize(heightMeasureSpec);
    // ... take into account the parent's size as needed ...
    super.onMeasure(
        MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY), 
        MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));
}
acj
  • 4,821
  • 4
  • 34
  • 49
  • Thanks, I'll try that went get home. – user557240 Oct 20 '12 at 14:51
  • Hi, I've tried implementing it but so far I've not had any luck. My custom layout is put under a linear layout and when I put the above code I get a blank screen. – user557240 Oct 21 '12 at 01:18
  • Basically after the onDraw method I have a y-value float with a value of about 900. I would then like to use this value to set the view width. – user557240 Oct 21 '12 at 01:38
  • I think the LinearLayout can be safely removed. Can you post some of your code so that we can see how you've integrated `onMeasure()`? What values are you using for `newWidth` and `newHeight`? – acj Oct 21 '12 at 02:41
  • Nope, if I take out the linear layout the view does not display. The values for newWidth are obtained after the onDrawMethod and for most objects it is around 900, though for some it can be as less as 300. – user557240 Oct 21 '12 at 17:05
  • It's hard to say what's going wrong. There are some variants on the above code [here](http://stackoverflow.com/questions/7423082/authorative-way-to-override-onmeasure) that might work for you. (In particular, heed the advice in Grimmace's answer.) Simplifying your layout might make the problem easier to solve, even if it initially appears to break the custom View. – acj Oct 21 '12 at 18:55
  • Ok, I've gone onMeasure working, but the problem appears to be that it is called before the onDraw() method. My height value is calculated from getHeight() but this is 0 when onMeasure is called. Surely there is a better way to do this. It's very frustrating. – user557240 Oct 21 '12 at 20:16
  • You can request a refresh of the view's measurements by calling `requestLayout()` on your View, which should trigger another `onMeasure()` and will give you some control over the order of events. `onMeasure()` may be called several times while the layout sorts out the sizes of its children. – acj Oct 21 '12 at 20:29