I want an action to be performed when the view is touched. However, the touches do not respond. The app doesn’t crash, it just seems to ignore it.
public class CustomDrawableView extends View implements OnTouchListener
{
static final int width = 100;
static final int height = 50;
public CustomDrawableView(Context context)
{
super(context);
setFocusable(true);
setOnTouchListener(mCustomDrawableView);
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN )
{
x = 400;
return true;
}
else {
x = 300;
return false;
}
}
protected void onDraw(Canvas canvas)
{
int mCanvasHeight = canvas.getHeight();
int mCanvasWidth = canvas.getWidth();
canvas.save();
canvas.rotate(R,x,y);
if (y >= mCanvasHeight-100) {
y = 0;
}
RectF oval = new RectF(x, y, x + width, y
+ height); // set bounds of rectangle
Paint p = new Paint(); // set some paint options
p.setColor(Color.BLUE);
canvas.drawOval(oval, p);
canvas.restore();
invalidate();
}
}
I have tried a bunch of different code to fix it. None of it does anything except if I change setOnTouchListener(mCustomDrawable) to mCustomDrawableView.setOnTouchListener(this) the app crashes. There is a bunch more code in the activity that I did not put up.
I’m guessing you believe that the
onTouch()isn’t firing because you’re not seeing your graphics change in response to changes inxor whatever. If that’s the case, it looks like you’re missing a call toinvalidate()in your touch handler to cause theViewto redraw itself again (via a call toonDraw()).Also, you have an
invalidate()actually insideonDraw()itself which really shouldn’t be there. It would certainly cause yourViewto redraw itself over and over – I suppose – so actually, I guess you should be seeing updates because that’s there. But that isn’t the way you should make aViewanimate; you should instead use aThreadorHandlerto schedule a regular, periodicinvalidate()– or some other means to regularly schedule an update.Also there should be no need to implement ‘OnTouchListener’ when you can just override
onTouch()as you have done. There are a couple of ways you can detect touch events for aView: (1) OverrideonTouch()as you have done, to get touch events on thatView. (2) Register a listener usingsetOnTouchListener(). This latter option enables you to have a listener that listens to touch events from oneViewor multipleViews, and it also ‘sees’ touch events before a registeredView‘s ownonTouch()sees them.Another thing I see is that you’re setting
xto 400 when you get an ACTION_DOWN event but then you’re setting it to 300 for any other kind of event such as an ACTION_MOVE. Considering it’s actually quite difficult to keep your finger still enough to never cause a string of ACTION_MOVE events immediately after an ACTION_DOWN, perhaps you’re just never seeing the graphics whenxis at 400 or something.You really need to post a complete example (for instance your code as you’ve posted has
setOnTouchListener(mCustomDrawableView)wheremCustomDrawableViewhas not been defined anywhere) together with specific errors from Logcat. Also, you should use the debugger to see if youronTouchever executes (stick a breakpoint in it).Also, you should accept some answers.