title: Adding a custom rectangle to a custom RelativeLayout
body: <p>I have a custom RelativeLayout, where i want to dynamically create Rectangles.</p>
  
  <p>They arent shown in my current code, but i dont know whats the reason.</p>
  
  <p>Custom RelativeLayout:</p>
  
  <pre><code>public class DrawView extends RelativeLayout {
  
      public DrawView(Context context) {
          super(context);
          setFocusable(true);
  
          this.addView(new Rectangle(this.getContext()));
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          super.onDraw(canvas);   
  
      }
  </code></pre>
  
  <p>Custom Rectangle:</p>
  
  <pre><code>public class Rectangle extends View {
  
      public Rectangle(Context context) {
          super(context);
      }
  
      public Rectangle(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      public Rectangle(Context context, AttributeSet attrs, int defStyle) { 
          super(context, attrs, defStyle); 
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          super.onDraw(canvas);
          Paint paint = new Paint();
          paint.setColor(Color.WHITE);
  
          canvas.drawRect(new Rect(), paint);
  
      }
  
  }
  </code></pre>
  
  <p>EDIT:
  Solution was:</p>
  
  <pre><code>public class Rectangle extends View {
  
      public Rectangle(Context context) {
          super(context);
          init();
      }
  
      public Rectangle(Context context, AttributeSet attrs) {
          super(context, attrs);
          init();
      }
  
      public Rectangle(Context context, AttributeSet attrs, int defStyle) { 
          super(context, attrs, defStyle); 
          init();
      }
  
      private void init() {
          this.setBackgroundResource(R.drawable.rectangle);
  
      }
  
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  
          int width = 150;
          int height = 50;
  
          setMeasuredDimension(width, height);
      }
  
  }
  
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle"&gt;
      &lt;corners
          android:radius="5sp"/&gt;
      &lt;gradient
          android:angle="315"
          android:startColor="#FFFF6666"
          android:endColor="#FFFF1111"
          android:type="linear"
          /&gt;
  &lt;/shape&gt;
  </code></pre>