3

I have a simple ImageButton in my Android program which, when clicked, appends a "0" in a TextView. When this button is long clicked, it is supposed to append "+" in that TextView. The program works fine but I'm facing a typical key bouncing effect. When I long press the button, it do appends a "+", but when I release the button, it also appends a "0". It seems like Android registers a second single click when long click ends. How can I eliminate this? Here's what I'm doing:

ImageButton button0=(ImageButton)V.findViewById(R.id.imageButtonzero);
        button0.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                enterNumber.append("0");

            }
        });
        button0.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                enterNumber.append("+");
                return false;
            }
        });

Thanks for your help!

Vinit Shandilya
  • 1,643
  • 5
  • 24
  • 44

1 Answers1

1

You need to return true in the OnLongClickListener, to inform other listeners that the event has been consumed and does not need to be actioned upon further down the line :

button0.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        enterNumber.append("+");
        return true;
    }
});

Source of information : Android javadoc

2Dee
  • 8,609
  • 7
  • 42
  • 53