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.
This is the View class :
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()>70 && event.getX()<130 && event.getY()>70 && event.getY()<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);
}
}
And here is my starting activity :
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);
}
}