Note that there are some explanatory texts on larger screens.

plurals
  1. PODetecting if a MouseEvent from onTouch is inside a circle drawn to the canvas of a custom view.
    primarykey
    data
    text
    <p>I'm trying to start with an android application and before that I have a need to do the following thing : I have a class extending from the View class and Implementing the View.onTouchListener interface. Now, I have drawn a circle in the onDraw() method so that the circle appears on the start of the application. Now, I would want to do something when the user touches(Actually clicks) the circle. So, the first thing I'm trying to do here is to display a toast msg on the touch of the circle. I tried using the following code but nothing happened. Pls help me find a solution.</p> <p>This is the View class : </p> <pre><code> package com.exam.trial; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; public class pad extends View implements View.OnTouchListener{ Paint paint; public pad(Context context) { super(context); // TODO Auto-generated constructor stub paint = new Paint(); } public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_DOWN: { if (event.getX()&gt;70 &amp;&amp; event.getX()&lt;130 &amp;&amp; event.getY()&gt;70 &amp;&amp; event.getY()&lt;130) { Toast toast = Toast.makeText(getContext(), "Works fine", Toast.LENGTH_SHORT); toast.show(); } return true; } } return false; } @Override public void onDraw(Canvas canvas) { paint.setColor(Color.YELLOW); canvas.drawCircle(100, 100, 50, paint); } } </code></pre> <p>And here is my starting activity : </p> <pre><code>package com.exam.trial; import android.app.Activity; import android.os.Bundle; public class TrialActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); pad p = new pad(this); setContentView(p); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload