Question

Can I really use these setOnLongClickListener and setOnClickListener for same button? Because if I long click the button both longclick and normal click will be executed and I dont know why. Can I really do this? Please help me:)

              readDbButton.setOnLongClickListener(
                      new View.OnLongClickListener() {
                          public boolean onLongClick(View view) {
                                //do something
                              return false;
                          }
                      }
              );                  

              readDbButton.setOnClickListener(
              new View.OnClickListener()
              {
                  public void onClick(View view)
                  {
                        //Do something else
                  }
              });
Was it helpful?

Solution

return TRUE in your onLongClick method so that the event will be consumed.

  readDbButton.setOnLongClickListener(
                      new View.OnLongClickListener() {
                          public boolean onLongClick(View view) {
                                //do something
                              return true;
                          }
                      }
              );   

OTHER TIPS

Try this proper way to Implement this

public class MainActivity extends Activity {

    private Button button;
    private GestureDetector gestureDetector;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gestureDetector = new GestureDetector(this, new MyGestureDetector());
        button = (Button) findViewById(R.id.button);

        button.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent ev) {
                return gestureDetector.onTouchEvent(ev);
            }
        });
    }

    private class MyGestureDetector extends SimpleOnGestureListener {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            Toast.makeText(MainActivity.this, "Single Tap", Toast.LENGTH_SHORT).show();
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            Toast.makeText(MainActivity.this, "Long Tap", Toast.LENGTH_SHORT).show();
        }

    }

}

I got the solution of your Question.Return true instead of false in LongPressed.Just see below:-

   readDbButton.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {

            Toast.makeText(HomeSafeActivity.this, "Long preess", Toast.LENGTH_LONG).show();

            return true;
        }
    });

That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-click listeners

readDbButton.setOnLongClickListener(
                      new View.OnLongClickListener() {
                          public boolean onLongClick(View view) {
                                //do something
Toast.makeText(getBaseContext(), "Long click", Toast.LENGTH_SHORT).show();
                              return true;
                          }
                      }
              );                  

              readDbButton.setOnClickListener(
              new View.OnClickListener()
              {
                  public void onClick(View view)
                  {
                       Toast.makeText(getBaseContext(), "onclick", Toast.LENGTH_SHORT).show();
                  }
              });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top