3

I've got a Custom View where I draw a grid onto a canvas. I want the grid to be drawn so that the outer edges of the grid coincide with the edges of the device screen. I use the getWidth() and getHeight() methods to determine the dimensions of my grid and then draw it, but the grid that appears always has cells drawn off-screen.

If I get the width and height of the display and use those for drawing instead then the width of my grid will be what I want, but the height is still off because some the height of the display is taken up by battery and wifi indicators, etc. Also, I don't want to use these values as I want to be able to embed my view in larger xml layouts.

Below is the code where I find my view width and height in my custom view:

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
    cellWidth = w/nCols;
    cellHeight = h/nRows;
    //find view dimensions
    viewWidth = getWidth();     //this is just equal to w
    viewHeight = getHeight();   //this is just equal to h
    super.onSizeChanged(w,h,oldw,oldh);
}

and the onDraw of my custom view:

@Override 
protected void onDraw(Canvas canvas){
    super.onDraw(canvas);

    canvas.drawRect(0,0,viewWidth,viewHeight, background);    

    for(int i=0; i <= nRows; i++){
        canvas.drawLine(0,i*cellHeight, nCols*cellWidth,i*cellHeight,lines);
        canvas.drawLine(i*cellWidth, 0, i*cellWidth, nRows*cellHeight, lines);
    }
}       

}

The approach I've followed is similar to this but has not worked. How can I get the true values for the width and height of my view? Thanks for reading!

Rookatu
  • 1,487
  • 3
  • 21
  • 50

3 Answers3

1

I'm guessing you have already seen this?

Custom View Height and Width

Or perhaps this is a different problem.

Community
  • 1
  • 1
James Manes
  • 526
  • 1
  • 11
  • 23
  • Yes, I saw that post. In fact, the link I gave comes from that post. However I do believe the answer lies somewhere in the onMeasure function. I did not try everything suggested at that post yet. I will now – Rookatu Jun 06 '13 at 19:39
  • It's working now (more or less). Thanks for pointing me back there, and sorry I didn't fully read it last time. Cheers! – Rookatu Jun 06 '13 at 19:46
0

I would do it by subtracting the height of the status bar.

Have you seen this post?

Author suggests:

Rect rectangle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;

or alternatively,

public int getStatusBarHeight()
{
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height","dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}
Community
  • 1
  • 1
dberm22
  • 3,187
  • 1
  • 31
  • 46
  • Well, the problem is that I want this to work even if the custom view is embedded in a larger xml layout. For instance, I may want to include buttons at the top or borders on the side. The real issue is that I can't seem to get the height and width of just the view itself. Thanks – Rookatu Jun 06 '13 at 19:25
0

When it comes to canvas there's several things that could be causing the issue, but just at first glance i can notice that you are trying to use the getWidth before the onSizeChanged has been passed to the superclass to do the proper calculations, in stead of calling super.onSizeChanged(w,h,oldw,oldh); at the end of the method, try by doing it like this:

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
    super.onSizeChanged(w,h,oldw,oldh);
    cellWidth = w/nCols;
    cellHeight = h/nRows;
    //find view dimensions
    viewWidth = getWidth();     //this is just equal to w
    viewHeight = getHeight();   //this is just equal to h
}

This is what i saw at first glance, but truth is that when it comes to canvas you have to be very explicit on sizes by your self, and do not expect the parent to handle any size based on your draws...

Regards!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54