Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Can I use setOnLongClickListener and setOnClickListener for same button?

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
                  }
              });
like image 631
user2034859 Avatar asked May 14 '14 05:05

user2034859


People also ask

What does setOnClickListener do in Android?

setOnClickListener(this); means that you want to assign listener for your Button “on this instance” this instance represents OnClickListener and for this reason your class have to implement that interface. If you have more than one button click event, you can use switch case to identify which button is clicked.

What is setOnClickListener in Java?

setOnClickListener it is actually creating an Anonymous Inner Class which implements OnClickListener. And the method onClick must need to be declared since its an abstract method inside that interface class. Any code you write inside onClick will be executed when the button is pressed.

Why my setOnClickListener is not working?

You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener() . Show activity on this post.


3 Answers

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;
                          }
                      }
              );   
like image 118
null pointer Avatar answered Nov 09 '22 22:11

null pointer


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();
        }

    }

}
like image 20
Biraj Zalavadia Avatar answered Nov 09 '22 23:11

Biraj Zalavadia


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

like image 35
Zar E Ahmer Avatar answered Nov 09 '22 23:11

Zar E Ahmer