Q:

Adding 5 rectangles drawn by drawRect method in canvas to layout.xml in android

I created 5 rectangles with different colors on the screen using drawRect method in Canvas. I want to add a seek bar to the bottom of the screen that changes the color of the rectangles gradually as it moves from left to right. I am having a problem in adding the rectangles to the layout and the program crashes.
Here is my rectangle class that draws 5 rectangles:  
public class RectAngle extends View {
    public RectAngle(Context context) {
        super(context);
        setFocusable(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float x = getWidth();
        float y = getHeight();

        Paint topLeftRect = new Paint();
        Paint bottomLeftRect = new Paint();
        Paint topRightRect = new Paint();
        Paint midRightRect = new Paint();
        Paint bottomRightRect = new Paint();

        // Draw  the top left rectangle
        topLeftRect.setStyle(Paint.Style.FILL);
        topLeftRect.setColor(Color.parseColor("#FFFF99"));
       // canvas.drawPaint(topLeftRect);
        canvas.drawRect(0, 0, x / 2, y / 2, topLeftRect);

        //Draw the bottom left rectangle

        bottomLeftRect.setStyle(Paint.Style.FILL);
        //bottomLeftRect.setColor(Color.WHITE);
       // canvas.drawPaint(bottomLeftRect);
        bottomLeftRect.setColor(Color.parseColor("#FFFFFF"));
        canvas.drawRect(0, y / 2, x / 2, y, bottomLeftRect);

        //Draw the top tight rectangle

        topRightRect.setStyle(Paint.Style.FILL);
        //topRightRect.setColor(Color.WHITE);
        topRightRect.setColor(Color.parseColor("#FF6600"));
        //canvas.drawPaint(topRightRect);
        canvas.drawRect(x / 2, 0, x, y / 3, topRightRect);

        // Draw the middle right rectangle

        midRightRect.setStyle(Paint.Style.FILL);
        //midRightRect.setColor(Color.WHITE);
        midRightRect.setColor(Color.parseColor("#66FFFF"));
       // canvas.drawPaint(midRightRect);
        canvas.drawRect(x / 2, y/3, x, 2*y / 3, midRightRect);

        //Draw the bottom right rectangle

        bottomRightRect.setStyle(Paint.Style.FILL);
        //bottomRightRect.setColor(Color.WHITE);
        bottomRightRect.setColor(Color.parseColor("#CCCC00"));
       // canvas.drawPaint(bottomRightRect);
        canvas.drawRect(x/2, 2*y/3, x, y, bottomRightRect);

    }
}

Here is my main activity class:
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(new RectAngle(this));
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

I know that setContentView(new RectAngle(this)) call draws the rectangles on the screen, but it doesn't add it xml file to be used by the seek bar.
Here's my activity_main.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <com.example.hassan.modernartui.RectAngle
        android:id="@+id/rectangle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <SeekBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:max="100" />
</RelativeLayout>

Layout Image
Thanks for your help.

A:

I think the problem is that u have not defined the right constructor.
Add the other 2 constructors after the 1st one like this 
public RectAngle(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public RectAngle(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

