Drawing circle at the touched position of View Canvas

June 24, 2013

In this article, we will develop an Android application that draws a circle at the touched position of a custom view canvas.

This application is developed in Eclipse 4.2.0 with ADT Plugin (22.0.1) and Android SDK ( 22.0.1 ) .

1. Create a new Android application project namely “GraphicsDrawPointViewCanvas”

New Android application project

Figure 1 : New Android application project

2. Configure the project

Configure the project

Figure 2 : Configure the project

3. Design application launcher icon

Design application launcher icon

Figure 3 : Design application launcher icon

4. Create a blank activity

Create a blank activity

Figure 4 : Create a blank activity

5. Enter MainActivity details

Enter MainActivity details

Figure 5 : Enter MainActivity details

6. Update the file res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
    <string name="app_name">Drawing Point - View Canvas</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="coordinates">Touch at some point in the canvas</string>

7. Create a new class in src/in/wptrafficanalyzer/graphicsdrawpointviewcanvas/PaintView.java

package in.wptrafficanalyzer.graphicsdrawpointviewcanvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class PaintView extends View implements OnTouchListener{

    Paint mPaint;
    float mX;
    float mY;
    TextView mTVCoordinates;

    public PaintView(Context context,AttributeSet attributeSet){

        /** Initializing the variables */
        mPaint = new Paint();
        mX = mY = -100;
        mTVCoordinates = null;

    protected void onDraw(Canvas canvas) {

        // Setting the color of the circle

        // Draw the circle at (x,y) with radius 15
        canvas.drawCircle(mX, mY, 15, mPaint);

        // Redraw the canvas

    public void setTextView(TextView tv){
        // Reference to TextView Object
        mTVCoordinates = tv;

    public boolean onTouch(View v, MotionEvent event) {
            // When user touches the screen
            case MotionEvent.ACTION_DOWN:
                // Getting X coordinate
                mX = event.getX();
                // Getting Y Coordinate
                mY = event.getY();

                // Setting the coordinates on TextView
                    mTVCoordinates.setText("X :" + mX + " , " + "Y :" + mY);
        return true;

8. Update the layout file res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context=".MainActivity" >

        android:layout_alignParentTop="true" />

        android:layout_below="@id/tv_coordinates" />


9. Update the file src/in/wptrafficanalyzer/graphicsdrawpointviewcanvas/MainActivity.java

package in.wptrafficanalyzer.graphicsdrawpointviewcanvas;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {

        // Getting reference to PaintView
        PaintView paintView = (PaintView)findViewById(R.id.paint_view);

        // Getting reference to TextView tv_cooridinate
        TextView tvCoordinates = (TextView)findViewById(R.id.tv_coordinates);

        // Passing reference of textview to PaintView object to update on coordinate changes

        // Setting touch event listener for the PaintView


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

10. Screenshot of the application

Showing circle at the touched position

Figure 6 : Showing circle at the touched position

11. Download the source code

How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , , ,

One Response to Drawing circle at the touched position of View Canvas

  1. yagnik on May 31, 2016 at 12:44 pm

    help to store multiple coordinates in array and show all of it on click button click event in the main activity….

Leave a Reply to yagnik Cancel reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner