13

I need your help if any one can be, it will be great thing for my solution. I don't know is it possible or not, but I want to try to fix this out any how.. Actually I want to implement two method on single button click event, its simple click and long click, here my code ::

homebutton = (ImageButton) findViewById(R.id.home_icon);
homebutton.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
        Intent intent = new Intent(context, MainActivity.class);
        startActivity(intent);
    }
});
homebutton.setOnLongClickListener(new OnLongClickListener() {
    public boolean onLongClick(View arg0) {
        Toast.makeText(getApplicationContext(), "Long Clicked " , Toast.LENGTH_SHORT).show();
        return false;
    }
});

So, here i am getting something wrong, even single click is working perfectly, and long click is also working, but problem is that after long click event its also start MainActivity as defined in above code of onClick method..

That should not be done, return false is also there, still not working as i want.. So, anybody please help me to get it resolve..

Thanks in Advance..

jt.
  • 253
  • 2
  • 5
  • 16

1 Answers1

41

I believe you need to return TRUE in your onLongClick method - telling the framework that the touch event is consumed and no further event handling is required.

homebutton.setOnLongClickListener(new OnLongClickListener() {
    public boolean onLongClick(View arg0) {
        Toast.makeText(getApplicationContext(), "Long Clicked " ,
              Toast.LENGTH_SHORT).show();

        return true;    // <- set to true
    }
});
waqaslam
  • 64,866
  • 15
  • 157
  • 170
  • Thanks buddy, it worked perfectly.. silly mistake in understanding code.. but thanks.. – jt. Nov 20 '12 at 09:53
  • Was that the Maps API v1? I'm working with v2; it's `OnMapLongClickListener` and the `onMapLongClick(LatLng pos)` method returns `void`. – Brian White Dec 05 '13 at 01:49