0

I have a Button which sets both onLongClickListener & onClickListener :

onLongClickListener :

 @Override public boolean onLongClick(View v) { do something ... return true; } 

onClickListener :

 @Override public void onClick(View v) { do something else ... } 

When I long click the button, onLongClick fires repeatly (sometimes onClick fires too when I release the button, it's weird @@") What I want is to make the onLongClick be triggered only once for one long press. So I modified the code :

onLongClickListener :

 @Override public boolean onLongClick(View v) { do something ... myButton.setLongClickable(false); return true; } 

onClickListener :

 @Override public void onClick(View v) { myButton.setLongClickable(true); do something else ... } 

Unfortunately, the onClick callback was locked too after onLongClick fires! I cant unlock the button anymore :| Whats wrong with my code? Also, why onClick sometimes works when I release my button after a long click?

1 Answer 1

1

I've got the code you need, give me a minute to post it.

 btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { //DO STUFF GRAH! } }); btn.setOnLongClickListener(new OnLongClickListener() { public boolean onLongClick(View v) { //OTHER STUFF return true; } }); 

This worked fine for me. I made an int and onLongClick added one and displayed it in a toast. Always incremented by one, and didn't do the onClick (reset it to 0).

Sign up to request clarification or add additional context in comments.

3 Comments

Eclipse was taking it's time, sorry.
Actually it's 100% the same as my original code, but it still can not work on my emulator :|
What API level are you testing on? This worked perfectly on 2.2 for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.