user557240
user557240

Reputation: 273

Android Custom view and canvas size

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

Upvotes: 1

Views: 3620

Answers (1)

acj
acj

Reputation: 4821

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));
}

Upvotes: 3

Related Questions