nambar
nambar

Reputation: 573

How to fill parent when drawing on canvas

I have a custom view (android) which draws a custom progress bar. I want the progress bar to fill its parent view (width). The problem that I have is that I don't know how the custom view can get its real limits. it needs to know the width limits in order to display different colors in the right ratio. I've implemented onMeasure() in the following way:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    setMeasuredDimension(widthMeasureSpec, 15);
}

Then, in my onDraw(), when I call getWidth() I get: 1073742246. How can I know the real parent bounds? obviously I did something wrong here... the view layout params are set to MATCH_PARENT/FILL_PARENT and I can draw on the entire area but I don’t know the real width in order to draw the bars in the right proportion.

Thanks,

Noam

Upvotes: 3

Views: 1934

Answers (2)

Knickedi
Knickedi

Reputation: 8787

This value contains the needed width, but it also contains MeasureSpec flags.

Use MeasureSpec.getSize() to strip the flags.

Upvotes: 2

hrnt
hrnt

Reputation: 10142

widthMeasureSpec includes other information than just the width in pixels. It looks like onMeasure is trying to pass you 422px as width with the mode MeasureSpec.EXACTLY.

Use MeasureSpec.getSize(widthMeasureSpec); to get the actual width in pixels. For more details, please see http://developer.android.com/reference/android/view/View.MeasureSpec.html

Upvotes: 2

Related Questions