Q: Invalidate does not call onDraw i need to by clicking on the button redraw the picture, but after method invalidate onDraw method is not called. But the call onDraw only happens after running the application.
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Draw draw = new Draw(this);

    Button button = (Button) findViewById(R.id.btn);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            draw.setA(40);
            draw.setB(300);
            draw.invalidate();
        }
    });

}
}

Here is the code Draw.java
public class Draw extends View {

private Paint mPaint;
private int a;
private int b;

public Draw(Context context) {
    super(context);
    init();
}

public Draw(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public Draw(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    mPaint = new Paint();
    mPaint.setColor(Color.BLACK);
    mPaint.setStrokeWidth(5);
    setWillNotDraw(false);
    a = 0;
    b = 0;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(Color.WHITE);
    canvas.drawPaint(mPaint);

    mPaint.setColor(Color.BLACK);
    mPaint.setAntiAlias(true);
    canvas.drawLine(80, 50, 80, 500, mPaint);
    canvas.drawLine(80, 50, 70, 85, mPaint);
    canvas.drawLine(80, 50, 90, 85, mPaint);

    canvas.drawLine(80, 500, 500, 500, mPaint);
    canvas.drawLine(500, 500, 465, 510, mPaint);
    canvas.drawLine(500, 500, 465, 490, mPaint);

    mPaint.setTextSize(35);
    mPaint.setStrokeWidth(2);
    canvas.drawText("X", 480, 540, mPaint);
    canvas.drawText("Y", 45, 80, mPaint);
    canvas.drawText("0", 70, 540, mPaint);

    drawFunction(canvas, a, b);
}

public void drawFunction(Canvas canvas, int a, int b) { 

    mPaint.setColor(Color.BLACK);
    mPaint.setAntiAlias(true);
    canvas.drawLine(80, 500, a, b, mPaint);
}

public void setA(int a) {
    this.a = a;
}

public void setB(int b) {

    this.b = b;
}
}

main.xml
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <labs.example.function.Draw
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </labs.example.function.Draw>
</RelativeLayout>

Thanks in advance.

A: Add an id
<labs.example.function.Draw
    android:id="@id/draw"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</labs.example.function.Draw>

Replace
 final Draw draw = new Draw(this);

by
 final Draw draw = (Draw) findViewById(R.id.draw); // need to refer to the view in xml.

If you use final Draw draw = new Draw(this); you need to add that view to your view hierachy.

A: Did you try to call setWillNotDraw(false). In this post you can see that by default all ViewGroup sub-classes do not call their onDraw method.
