3

I'm relatively a rookie/beginner with Java/Android programming. I've been trying to make it so while I press a given button in my application it produces a DTMF tone, but when I try to use setOnTouchListener the Android Studio shows me that error. It also gives me an error for MotionEvent which states Expression expected

Here are the important parts of the code:

boolean pressedCCW = false;
    class SendCCWTone extends AsyncTask<Void,Void,Void>{
        @Override
        protected Void doInBackground(Void... arg0){
            ToneGenerator toneGen;
            toneGen = new ToneGenerator(AudioManager.STREAM_DTMF,100);
            while(pressedCCW){
                toneGen.startTone(ToneGenerator.TONE_DTMF_1);
            }
            toneGen.stopTone();
            toneGen.release();
            createLog("CCW");
            return null;
        }
    }

final Button buttonCCW = (Button) findViewById(R.id.counter_clockwise);
    buttonCCW.setOnTouchListener(new View.OnTouchListener(){// Where the error is
        @Override
        public boolean onTouch(View v, MotionEvent event){// Where the other error is located
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    if(pressedCCW == false){
                        pressedCCW = true;
                        new SendCCWTone().execute();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    pressedCCW = false;
            }
            return true;
        }
    });
alexvandv
  • 31
  • 1
  • 6

4 Answers4

1

You are creating OnTouchListener inside of setOnClickListener. If you need TouchListener then you should register using setOnTouchListener instead of setOnClickListener

buttonCCW.setOnTouchListener(new View.OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event){
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    if(pressedCCW == false){
                        pressedCCW = true;
                        new SendCCWTone().execute();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    pressedCCW = false;
            }
            return true;
        }
    });
Ahmad Nawaz
  • 1,129
  • 7
  • 26
0

This problem can be solved if you place setOnTouchListener inside the onCreate() method of your activity.

Kundan
  • 590
  • 9
  • 21
-1

Try to add this to your code :

 implements View.OnTouchListener

and use setOnTouchListner instead of setOnClickListener.

buttonCCW.setOnListener(new View.OnTouchListener(){// Where the error is
    @Override
    public boolean onTouch(View v, MotionEvent event){// Where the other error is located
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(pressedCCW == false){
                    pressedCCW = true;
                    new SendCCWTone().execute();
                }
                break;
            case MotionEvent.ACTION_UP:
                pressedCCW = false;
        }
        return true;
    }
});
Sushrita
  • 725
  • 10
  • 29
  • Sushrita, where should I exactly put 'implements View.OnTouchListener'? Thanks! :) – alexvandv Jun 08 '15 at 04:22
  • At your class starting Like this : 'public class MainActivity extends ActionBarActivity implements View.OnTouchListener {' – Sushrita Jun 08 '15 at 04:29
-1

Rather than use setOnClickListener, you can set onClick in the XML and point to a method (it does the same thing and looks nicer). In this case, you'd have a method like produceSound:

public void produceSound(View view) {
  // your onClick method
}

and in the activity's XML, find where that button, counter_clockwise, is and add: android:onClick="produceSound" to the button's XML.

More here if you're curious: How exactly does the android:onClick XML attribute differ from setOnClickListener?

However, if you're using onTouch, then you will have to stick with what everyone else is suggesting. XML does not support an android:onTouch attribute.

Community
  • 1
  • 1
nonamorando
  • 1,556
  • 2
  • 14
  • 33
  • Yeah, in this case I'm working with onTouch since I wish to have the application producing the sound while the button is held down. Thanks! – alexvandv Jun 08 '15 at 04:24
  • Post your answer if you've solved it--just for other people who may need this for reference. (And no problem as always.) – nonamorando Jun 08 '15 at 04:25