This is because you are calling the function square() from constructor. At that time the view won't be created and getWidth() and getHeight() inside square() method will be zero and thus the path will not be drawn over the Bitmap. You can get the view width and height from onMeasure method. I have changed your code, please find it below,
public class MyView extends View {
private Bitmap bmp;
private Path path;
private Paint paint;
private int viewWidth, viewHeight;
public MyView(Context context) {
super(context);
setMinimumHeight(400);
setMinimumWidth(400);
setupDrawing();
}
private void setupDrawing() {
path = new Path();
paint = new Paint();
paint.setColor(Color.GREEN);
}
public void square() {
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
bmp = Bitmap.createBitmap(200, 200, conf);
Canvas canvas = new Canvas(bmp);
path.moveTo(0, 0);
path.lineTo(viewWidth - 50, 0);
path.lineTo(viewWidth - 50, viewHeight - 50);
path.lineTo(0, viewHeight - 50);
path.lineTo(0, 0);
canvas.drawPath(path, paint);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
square();
if (bmp != null)
canvas.drawBitmap(bmp, 0, 0, paint);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getSuggestedMinimumWidth(),
getSuggestedMinimumHeight());
viewWidth = getSuggestedMinimumWidth();
viewHeight = getSuggestedMinimumWidth();
};
}