1

I have the following code in the onCreate method of an Activity:

GridLayout gridLayout = new GridLayout(this);
gridLayout.setColumnCount(5);
gridLayout.setRowCount(5);
for (int i = 0; i < 25; i++) {
    ImageView imageView = new ImageView(this);
    gridLayout.addView(imageView, i);
}
setContentView(gridLayout);

Now, I'd like for each ImageView in the GridLayout to respond to (listen for) a double-click. How can I go about doing this? (I've already used a long-press on one of the Views to perform a different action so I can't just use this instead, although I realize that would be easier since there's a native listener for it.)

kmell96
  • 1,365
  • 1
  • 15
  • 39
  • possible duplicate of [How can i place double click event on ImageView in Android](http://stackoverflow.com/questions/5191456/how-can-i-place-double-click-event-on-imageview-in-android) – royB Aug 25 '15 at 07:32

2 Answers2

0

You have two options:

1) Use a boolean with handler (like this answer)

  • add boolean doubleClick = false;
  • and Handler doubleHandler
  • in onClick check if doubleClick is true
    • if true, it is a double click
    • if not, set doubleClick to true and use the handlers postDelayed to set it back to false after i.e. 500ms

2) Use the GestureListener ( onDoubleTap() method )

Community
  • 1
  • 1
Rami
  • 7,879
  • 12
  • 36
  • 66
0

There might be a better way, but the first thing that jumps to my mind:

have a boolean flagging a recent click which is set to true when the view is clicked

Create a Runnable outside the onClick method (only ever have the one, don't make one for every click since android limits the amount of threads we can have. Also just good practice.)

Runnable doubleClick = new Runnable() {
  @Override
  public void run() {
      recentClick = false;
  }
}

then if this was the first click (recentClick is false), use

final Handler handler = new Handler();
handler.postDelayed(doubleClick, 100);

to reset the boolean after 100 milliseconds

of if onClick is triggered and the boolean is set true, you've had a double click.

user185578
  • 71
  • 7