title: Set Canvas Width and Height Android for Custom View
body: <p>I have an issue with a CustomView. I have a layout where I have EditText fields on top of screen. At the bottom there are two buttons. The remaining space in the middle of the screen is occupied with an ImageView. On the ImageView I should be able to draw a rectangle. I have kept a CustomView in my layout which also occupied the same width and height as that of ImageView. But my issue is the canvas occupies the whole width and height of a screen. So when user draws a rectangle on my image it hides below my EditText fields. I want to limit  my canvas size same as that of the ImageSize and not to hide. </p>
  
  <p>I have checked some sources but it did not help me.  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/white" &gt;
  
      &lt;RelativeLayout
          android:id="@+id/headerLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:paddingBottom="10dp"
          android:background="@color/header_red" &gt;
  
          &lt;TextView
              android:id="@+id/tvAddName"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_marginTop="10dp"
              android:layout_marginLeft="10dp"
              android:text="Enter Name"
              android:textColor="@color/white"
              android:textSize="16sp" /&gt;
  
          &lt;TextView
              android:id="@+id/tvName"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@id/tvAddDimens"
              android:layout_marginTop="5dp"
              android:layout_alignParentLeft="true"
              android:layout_marginLeft="10dp"
              android:text="Name:"
              android:textColor="@color/white"
              android:textSize="16sp" /&gt;
          &lt;EditText
              android:id="@+id/etNameValue"
              android:layout_width="80dp"
              android:layout_height="20dp"
              android:layout_marginLeft="10dp"
              android:layout_below="@id/tvAddDimens"
              android:layout_marginTop="5dp"
              android:layout_toRightOf="@id/tvWidth"
              android:paddingLeft="5dp"
              android:paddingTop="2dp"
              android:singleLine="true"
              android:paddingBottom="2dp"
              android:background="#FFFFFF"
              android:maxLength="8"
              android:textColor="@color/black"
              android:textSize="16sp" /&gt;
      &lt;/RelativeLayout&gt;
  
      &lt;RelativeLayout
          android:id="@+id/bottomLayout"
          android:layout_width="match_parent"
          android:layout_height="40dp"
          android:layout_alignParentBottom="true"
          android:background="@color/dark_gray" &gt;
  
          &lt;ImageView
              android:id="@+id/ivDelete"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="10dp"
              android:layout_alignParentLeft="true"
              android:layout_centerVertical="true"
              android:layout_marginLeft="50dp"
              android:src="@drawable/delete" /&gt;
  
          &lt;ImageView
              android:id="@+id/ivOk"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="10dp"
              android:layout_alignParentRight="true"
              android:layout_centerVertical="true"
              android:layout_marginRight="50dp"
              android:src="@drawable/tick" /&gt;
      &lt;/RelativeLayout&gt;
  
      &lt;RelativeLayout android:id="@+id/imgLayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_above="@id/bottomLayout"
          android:layout_below="@id/headerLayout"
          android:layout_marginBottom="10dp"
          android:layout_marginTop="10dp" &gt;
  
          &lt;ImageView
              android:id="@+id/ivCapturedImg"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_centerInParent="true"
              android:scaleType="fitXY" /&gt;
  
          &lt;com.ibee.macromedia.utils.DrawingView
              android:id="@+id/drawRect"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_margin="10dp" /&gt;
      &lt;/RelativeLayout&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>My DrawingView class is:</p>
  
  <pre><code>public class DrawingView extends View {
  
  /** Need to track this so the dirty region can accommodate the stroke. **/
  private static final float STROKE_WIDTH = 5f;
  
  public Paint paint;
  /**
   * Optimizes painting by invalidating the smallest possible area.
   */
  private int mStartX = 0;
  private int mStartY = 0;
  private int mEndX = 0;
  private int mEndY = 0;
  private boolean isDraw=false;
  private Context mContext;
  
  public DrawingView(Context context, AttributeSet attrs) {
      super(context, attrs);
      this.mContext=context;
      init();
  
  }
  
  public void init(){
      paint=new Paint();
      paint.setAntiAlias(true);
      paint.setColor(mContext.getResources().getColor(R.color.fluor));
      paint.setStyle(Paint.Style.STROKE);
      paint.setStrokeWidth(STROKE_WIDTH);
  }
  
  /**
   * Erases the signature.
   */
  public void clear() {
      isDraw=true;
      paint=new Paint();
      paint.setAntiAlias(true);
      paint.setColor(Color.TRANSPARENT);
      paint.setStyle(Paint.Style.STROKE);
      paint.setStrokeWidth(STROKE_WIDTH);
      invalidate();
  }
  @Override
  protected void onDraw(Canvas canvas) {
  
      Log.e("MACROMEDIA", " DRAWING VIEW CANVAS WIDTH " + canvas.getWidth() + " HEIGTH " + canvas.getHeight());
      if(isDraw==true){
          paint = new Paint();
          paint.setColor(Color.TRANSPARENT);
          canvas.drawRect(Math.min(mStartX, mEndX), Math.min(mStartY, mEndY),
                  Math.max(mEndX, mStartX), Math.max(mEndY, mStartY), paint);
      } else{
          paint=new Paint();
          paint.setAntiAlias(true);
          paint.setColor(mContext.getResources().getColor(R.color.fluor));
          paint.setStyle(Paint.Style.STROKE);
          paint.setStrokeWidth(STROKE_WIDTH);
          canvas.drawRect(Math.min(mStartX, mEndX), Math.min(mStartY, mEndY),
                  Math.max(mEndX, mStartX), Math.max(mEndY, mStartY), paint);
      }
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
      float eventX = event.getX();
      float eventY = event.getY();
              isDraw=false;
      switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
          mStartX = (int) event.getX();
          mStartY = (int) event.getY();
          return true;
  
      case MotionEvent.ACTION_MOVE:
          final int x = (int) event.getX();
          final int y = (int) event.getY();
          if (Math.abs(x - mEndX) &gt; 5 || Math.abs(y - mEndY) &gt; 5) {
              mEndX = x;
              mEndY = y;
              invalidate();
          }
          break;
      case MotionEvent.ACTION_UP:
          break;
  
      default:
          return false;
      }
  
      invalidate();
  
      return true;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Please help me. Thanks</p>