0

If I implement the OnClickListener, the sound effect will work on clicking the button. While on implementing the OnTouchListener the sound effect doesn't work on touching it. So how to enable the sound effect on implementing the OnTouchListener?

EDIT:

Here is the code if I use clicking approach:

public class CalculatorActivity extends Activity implements OnClickListener
{
    //...
    private Button btn1;
    private EditText edLCD;
    //...

    public void onCreate(Bundle savedInstanceState)
    {
        //...
        btn1 = (Button) findViewById(R.id.d1);
        btn1.setOnClickListener(this);

        edLCD = (EditText) findViewById(R.id.edLCD);
    }

    //...

    public void onClick(View v)
    {
        edLCD.setText(edLCD.getText().toString() + "1");
    }
}

And here is the code if I use touching approach:

public class CalculatorActivity extends Activity implements OnTouchListener
{
    //...
    private Button btn1;
    private EditText edLCD;
    //...

    public void onCreate(Bundle savedInstanceState)
    {
        //...
        btn1 = (Button) findViewById(R.id.d1);
        btn1.setOnTouchListener(this);

        edLCD = (EditText) findViewById(R.id.edLCD);
    }

    //...

    public boolean onTouch(View v, MotionEvent event)
    {
        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            edLCD.setText(edLCD.getText().toString() + "1");
        }
        return true;
    }
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • 1
    Show your code. Also why do you want to do this for a 'touch' action and not for a 'click' action? – Squonk Feb 20 '12 at 00:38
  • @MisterSquonk Touching a button gives faster response, while clicking on it won't respond until you raise your finger from the screen. Besides, what code should I show to you? I just ask for the way of enabling the sound effect on touching the button. – Eng.Fouad Feb 20 '12 at 00:42
  • Just use onClick, it's not expected for some button to react onTouch and not onClick. Don't break the UX! – Daniel Lockard Feb 20 '12 at 00:48
  • @DanielLockard Actually, there is no difference between `onTouch` and `onClick`, they both are similar except for the responsive time. – Eng.Fouad Feb 20 '12 at 00:50
  • @Eng - show the code for the OnTouchListener that doesn't work. We can't tell you how to get it to work until we see what you have tried. – Squonk Feb 20 '12 at 00:53

1 Answers1

3

Try...

public boolean onTouch(View v, MotionEvent event)
{
    if(event.getAction() == MotionEvent.ACTION_DOWN)
    {
        v.playSoundEffect(SoundEffectConstants.CLICK);
        edLCD.setText(edLCD.getText().toString() + "1");
    }
    return true;
}
Squonk
  • 48,735
  • 19
  • 103
  • 135