I am creating a custom view which will have a bitmap so that user can draw in it and some normal Android buttons in the bottom for user interaction.
To size my bitmap (height of drawing areas should be 50% ) I am overriding
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(widthMeasureSpec, (int)(parentHeight * 0.50));
super.onMeasure(widthMeasureSpec, (int)(parentHeight * 0.50));
}
This gives me exception that
“java.lang.IllegalArgumentException: width and height must be > 0”
If I set super.onMeasure(widthMeasureSpec, heightMeasureSpec);
I cannot see my buttons places in the bottom.
If I dont write super.onMeasure() anything I draw is not seen once I release the mouse.
I am using a xml file for layout :
<view class="com.my.CustomView" android:id="@+id/myView"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_height="wrap_content"
android:text="B1" android:layout_width="wrap_content"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="B2"/>
</LinearLayout>
What else should I do ?
Wouldn’t be easier to give your custom view a size in dp. You then can see your layout in design mode. Then use onSizeChanged to find out the size of your canvas.
onMeasure is usually used when the size of the canvas depends on runtime values, like the result of a game or number of loaded items etc.
Here is an example that works:
Also the constructor for your custom vuew must include attribute set: