In addition to the ways already suggested, here's another way. If you know what the size of this view will be, or you can programmatically determine its size, you can override the onMeasure method which tells the OS how big the view is. For example if you're writing a game or something that uses a Canvas
then you can use the getHeight()
and getWidth()
methods, or you can get the size directly from whatever parameters you used when you created your canvas. onMeasure is easy to use:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
setMeasuredDimension(width, height);
}
Refer to the documentation if you need a bit more info about the parameters. (If it doesn't quite make sense, don't worry, it didn't to me either :P But you can use this without actually needing to worry about widthMeasureSpec and heightMeasureSpec as long as you can calculate the size the view should be correctly.)