0

I have four ImageButtons in my app.

enter image description here

Three smaller buttons can be either shown or "hidden" below the bigger button. I hide buttons using rotate and translate animations.

The problem is: OnClickListener's onClick method always gets triggered no matter a smaller button shown or not.

I mean, when smaller buttons are "hidden", touching the place on screen, where one of the smaller button resides when shown, triggers onClick method.

It looks like Android OS does not take actual placement of the button into account when deciding whether it should trigger onClick method or not.

How can I overcome the issue?

I want onClick method to be called ONLY when there is a button below my finger.

EDIT:

All suggested workarounds rely on hiding the button. This doesn't help at all. The onClick method gets called for INVISIBLE and GONE buttons too. I checked this in debugger.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130

7 Answers7

1

Looks like you are going for an Arc menu. Why not use the awesome implementations of Siyamed and DaCapricorn, instead of re-inventing the wheel?

Srikanth
  • 2,014
  • 19
  • 22
  • Thank you for the links. Projects look great and I might be using them in one of my other applications. – Bobrovsky Jan 13 '14 at 12:18
1

Did you set your click interface your activity and implemented onClickListener? If so you need to separated your buttons by setting if conditions like if(arg0 == button1) for all the buttons.

Barışcan Kayaoğlu
  • 1,294
  • 3
  • 14
  • 35
1

The issue was caused by the fact that tween animations (the original animation framework) animate the pixels, not the touch zones of a widget.

I have replaced tween animations with property animations and now everything works like I expect it to work.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
0

I'd suggest setting the button visibility to GONE after completing the animation.

UPDATE:

Another suggestion is to have a button that is not animated (for which the problem should not happen) and an image (graphically identical) that is animated after the button is hidden. The image should not receive any clicks, under the big button or not, and the real button should not receive clicks when hidden.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

Setting

btn.setVisibility(View.INVISIBLE);

doesn't call onClick method where buttons are not visible. I have tested it. So you might be registering onClick listener in unnecessary views.

Bipin Bhandari
  • 2,694
  • 23
  • 38
0

Try to set the listener using the property onClick of the button in the layout.xml (for example

android:onClick="onClickButtonCamera"

and then declare a public function with the same name like:

public void onClickButtonCamera(View view) {
    //some stuff...
}

I allways use it and the function is never called when the buttons are hidden. I hope it helps you.

0

i think there are three ways to solve this issue.

1) if you are setting onClick Listener then set it null when you hide buttons and set listener when you show them.

2)Add a checking in onclick for each button about their visibility simply return; the function when button's visibility is not VISIBLE(use the function button.getVisibility() != View.VISIBLE)

3)Last one not sure, usually setting gone as visibility will protect it from clicking.

Athul
  • 231
  • 1
  • 7