title: Placing a Custom View inside a ConstraintLayout
body: <p>I am new to implementing a custom view in Android. I have implemented a custom view (a simple rectangle). Here the code:</p>
  <pre><code>class CustomView @JvmOverloads constructor(
      context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
  ) : View(context, attrs, defStyleAttr) {
  
      private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
          // Paint styles used for rendering are initialized here. This
          // is a performance optimization, since onDraw() is called
          // for every screen refresh.
          style = Paint.Style.FILL
          textAlign = Paint.Align.CENTER
          textSize = 55.0f
          typeface = Typeface.create(&quot;&quot;, Typeface.BOLD)
          color = Color.GREEN
      }
  
      private val rectangle = RectF(100f, 100f, 500f, 120f)
  
      override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
          super.onSizeChanged(w, h, oldw, oldh)
      }
  
      override fun onDraw(canvas: Canvas?) {
          super.onDraw(canvas)
          canvas?.drawRect(rectangle, paint)
      }
  }
  </code></pre>
  <p>Due to my layout file, it should be placed in the middle of the screen:</p>
  <pre><code>&lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
  
      &lt;androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          tools:context=&quot;.CustomViewFragment&quot;&gt;
  
          &lt;com.example.learningcustomviewimplementation.CustomView
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;
              app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
              app:layout_constraintEnd_toEndOf=&quot;parent&quot;
              app:layout_constraintStart_toStartOf=&quot;parent&quot;
              app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  
      &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  &lt;/layout&gt;
  </code></pre>
  <p>But it is placed near the top left corner. From what I see in the output, the constraints are not used. What do I need to change so that the custom view is placed based on the constraints I use in the layout file ?</p>