androidonclicklisteneronlongclicklistener

setOnClickListener and setOnLongClickListener call on single button issue


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..


Solution

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