Search code examples
javaandroidandroid-canvaspaintcomponentdrawrect

Two rectangles next to eachother is not working (Java)


I am trying to draw a line, but I keep getting problems. I want to achieve something like this:

enter image description here

private Paint red = new Paint();
private Paint orange = new Paint();

red.setColor(Color.parseColor("#FF0000"));
orange.setColor(Color.parseColor("#FF8C00"));

canvas.drawRect(0, 400, 300, 0, red);
canvas.drawRect(300, 400, 300, 0, orange);

The orange bar just sits in the same spot as the red one... Why?


Solution

  • Can you see that the length of the orange rectangle (300-300) is 0 in your code. That is why you can't see it. So try this:

        canvas.drawRect(0, 400, 300, 0, red);
        canvas.drawRect(300, 400, 600, 0, orange);