title: How to use a custom class (which extends View class) in activity class to draw path without changing the other parts of the main layout in android
body: <p>I have the following two classes:
  1. ImageGalleryDemoActivity (to import an image from gallery) 
  2. DrawClass (To draw a freehand path on the imported image)</p>
  
  <pre><code>public class ImageGalleryDemoActivity extends Activity {
      private static int RESULT_LOAD_IMAGE = 1;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
          buttonLoadImage.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
  
                  Intent i = new Intent(
                          Intent.ACTION_PICK,
                          android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  
                  startActivityForResult(i, RESULT_LOAD_IMAGE);
              }
          });
      }
  
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          if (requestCode == RESULT_LOAD_IMAGE &amp;&amp; resultCode == RESULT_OK &amp;&amp; null != data) {
              Uri selectedImage = data.getData();
              String[] filePathColumn = { MediaStore.Images.Media.DATA };
  
              Cursor cursor = getContentResolver().query(selectedImage,
                      filePathColumn, null, null, null);
              cursor.moveToFirst();
  
              int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
              String picturePath = cursor.getString(columnIndex);
              cursor.close();
  
              ImageView imageView = (ImageView) findViewById(R.id.imgView);
              imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
  
  
  
          }
  
  
      }
  }
  </code></pre>
  
  <p><strong>and the following class to draw path on the imported image.</strong></p>
  
  <pre><code>public class DrawClass extends View implements OnTouchListener {
  
      private Paint paint;
      List&lt;Point&gt; points;
      int DIST = 2;
      boolean flgPathDraw = true;
  
      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.id.imgView);
  
      public DrawClass(Context c  ) {
          super(c);
          setFocusable(true);
          setFocusableInTouchMode(true);
  
          paint = new Paint(Paint.ANTI_ALIAS_FLAG);
          paint.setStyle(Paint.Style.STROKE);
          paint.setStrokeWidth(2);
          paint.setColor(Color.BLACK);
  
          this.setOnTouchListener(this);
          points = new ArrayList&lt;Point&gt;();
      }
      public DrawClass(Context context, AttributeSet attrs) {
          super(context, attrs);
          setFocusable(true);
          setFocusableInTouchMode(true);
  
          paint = new Paint(Paint.ANTI_ALIAS_FLAG);
          paint.setStyle(Paint.Style.STROKE);
          paint.setStrokeWidth(2);
          paint.setColor(Color.BLACK);
  
          this.setOnTouchListener(this);
          points = new ArrayList&lt;Point&gt;();
  
      }
  
      public void onDraw(Canvas canvas) 
      {
          canvas.drawBitmap(bitmap, 0, 0, null);
  
          Path path = new Path();
          boolean first = true;
  
          for (int i = 0; i &lt; points.size(); i += 2) 
          {
              Point point = points.get(i);
              if (first) {
                  first = false;
                  path.moveTo(point.x, point.y);
              } else if (i &lt; points.size() - 1) {
                  Point next = points.get(i + 1);
                  path.quadTo(point.x, point.y, next.x, next.y);
              } else {
                  path.lineTo(point.x, point.y);
              }
          }
          canvas.drawPath(path, paint);
      }
  
      public boolean onTouch(View view, MotionEvent event) {
          // if(event.getAction() != MotionEvent.ACTION_DOWN)
          // return super.onTouchEvent(event);
          Point point = new Point();
          point.x = (int) event.getX();
          point.y = (int) event.getY();
  
          if (flgPathDraw) {
              points.add(point);
          }
  
          invalidate();
          Log.e("Hi  ==&gt;", "Size: " + points.size());
  
          return true;
      }
      public void fillinPartofPath()
      {
          Point point = new Point();
          point.x = points.get(0).x;
          point.y = points.get(0).y;
  
          points.add(point);
          invalidate();
      }
      public void resetView()
      {
          points.clear();
          paint.setColor(Color.WHITE);
          paint.setStyle(Style.STROKE);
          flgPathDraw=true;
          invalidate();
      }
  }
  
  class Point {
      public float dy;
      public float dx;
      float x, y;
  
      @Override
      public String toString() {
          return x + ", " + y;
      }
  }
  </code></pre>
  
  <p><strong>Now, problem is that I can change whole main layout using setContentView, but I want to keep  the original layout with the "Load Picture" button and draw a path on the image. So, how can I use the DrawClass class in the activity class keeping the original layout?</strong></p>