Si8
Si8

Reputation: 9235

setOnTouchListener() is giving me an error

button.setOnTouchListener(new OnTouchListener()
{
  public void onClick(View v)
  {
      Toast.makeText(MainActivity.this, "YOUR TEXT", 5000).show();
  }
});

Eclipse states the following:

The method setOnTouchListener(View.OnTouchListener) in the type View is not applicable for the arguments (new OnTouchListener(){})

Imports:

import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast;

Any idea how to resolve it?

Upvotes: 0

Views: 17622

Answers (7)

MDubzem
MDubzem

Reputation: 109

I also had this problem and I had to do 'fix project setup' which I found after clicking the quick fixes option. I then added a .jar file and it was all good.

Upvotes: 0

amruzzzzzzzzzi
amruzzzzzzzzzi

Reputation: 63

you are putting an ontouchlistener with an onclick listener thats your mistake.

    button.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {

     Toast.makeText(MainActivity.this, "YOUR TEXT", 5000).show();
    return false;
    }
 });

Upvotes: 0

Srikanth Roopa
Srikanth Roopa

Reputation: 1790

and when u use new OnTouchListener() u need to override on onTouch not onClick or change it to
plus.setOnClickListener(new OnClickListener()

 @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            Toast.makeText(MainActivity.this, "YOUR TEXT", 5000).show();

            return false;
        }

Upvotes: 1

anthonycr
anthonycr

Reputation: 4186

The code

public void onClick(View v)
  {
      Toast.makeText(MainActivity.this, "YOUR TEXT", 5000).show();
  }

is not valid for an onTouchListener. That is why you are getting the error, you should be using

@Override
public void onTouch(View v, MotionEvent e)
      {
          Toast.makeText(MainActivity.this, "YOUR TEXT", 5000).show();
      }

instead if you really want an onTouchListener, although I highly suggest Chiral Code's suggestion of using an onClickListener

Upvotes: 1

Aleksandr
Aleksandr

Reputation: 787

Check your code: http://developer.android.com/reference/android/view/View.OnTouchListener.html You should use method onTouch.

Upvotes: 1

AZX
AZX

Reputation: 572

There are two solutions:

  1. Add import android.view.View.OnTouchListener; to the beginning of your class

  2. Replace button.setOnTouchListener(new OnTouchListener() with button.setOnTouchListener(new ViewOnTouchListener() as Chiral Code suggested.

Upvotes: 3

Chiral Code
Chiral Code

Reputation: 1436

Use this code:

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "YOUR TEXT", 5000).show();
        }
    });

Upvotes: 4

Related Questions