Search code examples
androidontouchlistenertouch-eventontouch

Android app does not get into onTouch method


I'm having troubles with the method onTouch(). The code I'm using to implement it is the following.

public class EjemploView extends View implements OnTouchListener  {
...
@Override
    public boolean onTouch(View v, MotionEvent event) {
            if(event.getActionMasked()==0) {
                slider2.setCenX(slider2.getCenX() - 1);
            }
        return false;
    }

}

The point is that I set a debugger stop inside onTouch method and even if I touch the screen it will never go inside it. What am I doing wrong?


Solution

  • Adding as an answer, simply so that it makes more sense. You are implementing onTouchListener but you dont set it as your onTouchListener. Thus you are left with a fully functioning onTouchListener in your view that does not ever get called.

    public EjemploView(Context context) {
            super(context);
            /** \/ Add this line right here \/ */
            setOnTouchListener(this);
            Resources res = context.getResources();
            drawableFader = res.getDrawable(R.drawable.fader);
            drawableSlider=res.getDrawable(R.drawable.slider);
    
            fader1 = new Grafico(this, drawableFader);
            fader2 = new Grafico(this, drawableFader);
            slider1 = new Grafico(this, drawableSlider);
            slider2 = new Grafico(this, drawableSlider);
    
    
            fader1.setAlto(350);
            fader1.setAncho(64);
            fader2.setAlto(350);
            fader2.setAncho(64);
            fader2.setAngulo(90);
    
    
            slider1.setAlto(90);
            slider1.setAncho(55);
            slider2.setAlto(90);
            slider2.setAncho(55);
            slider2.setAngulo(90);
    
    
        }