Views

Consideration for subclassing a View :

When customizing a view or layout by subclassing you implement the following basic 

public class MyView extends View {

    Paint paint;

    public MyView(Context context){

        this(context,null);

           }

    public MyView(Context context,AttributeSet attr){

        super(context,attr);


       paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        paint.setColor(Color.RED);


       // setWillNotDraw(false);//if onMeasure is not overridden

    }


    @Override

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight()/2, canvas.getWidth()/2, paint);

        // never gets called :-(

    }


    @Override

    protected void dispatchDraw(Canvas canvas) {

        super.dispatchDraw(canvas);

        // this gets called, but with a canvas sized after the padding.

    }

    @Override

    protected void onMeasure(int measureWidth,int measureHeigth)

    {

        setMeasuredDimension(100,100);//example

    }

}


if you dont override onMeasure you must call setWillNotDraw(false) in your constuctor (to have visibility of drawn view)

 © Xosrov 2016