0

What i have: I have a set of buttons in horizontal scroll view

What i want: I want to detect a event on double tapping the button

Note Usually i deal with click events using class implements Onclicklistener. But now how can i achieve the same for doubleclickevents. it will be helpful if there is any interface i can use for this purpose also


CODE:

<HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/button1"
                android:layout_width="200dp"
                android:layout_height="150dp"
                android:text="Song1" />

            <Button
                android:id="@+id/button2"
                android:layout_width="200dp"
                android:layout_height="150dp"
                android:text="Song2" />

            <Button
                android:id="@+id/button3"
                android:layout_width="200dp"
                android:layout_height="150dp"
                android:text="Song3" />
        </LinearLayout>
    </HorizontalScrollView>
Devrath
  • 42,072
  • 54
  • 195
  • 297

4 Answers4

3

Here's what is working for me,

Note: It will only work for Double click, it can't handle single click & double click actions seperately

// Define 2 global class variables
private static long LAST_CLICK_TIME = 0;
private final int mDoubleClickInterval = 400; // Milliseconds

// Then, inside onClick method of Button
long doubleClickCurrentTime = System.currentTimeMillis();
long currentClickTime = System.currentTimeMillis();
if (currentClickTime - LAST_CLICK_TIME <= mDoubleClickInterval)
{
    Toast.makeText(this, "Double click detected", Toast.LENGTH_SHORT).show();
}
else
{
    LAST_CLICK_TIME = System.currentTimeMillis();
    // !Warning, Single click action problem
}
0

There is an excellent answer here:

Single click and double click of a button in android

However, I suggest using the OnLongPress event, which is what comes recommended from Google.

View that API here:

http://developer.android.com/reference/android/view/View.OnLongClickListener.html

Cheers

Community
  • 1
  • 1
cyclical
  • 395
  • 4
  • 14
0

I use this solotion :

    var doubleTap = false
    your_button.setOnClickListener {
        if (doubleTap)
            handleClick(it.context)
        doubleTap = true
        Handler(Looper.getMainLooper()).postDelayed({
            doubleTap = false
        }, 1500)
    }

you can use this trick in onBackPress Activity for exit app in tow step.

Good Luck

Javid Sattar
  • 749
  • 8
  • 14
0
private int buttonClick = 0;
private void fabVisibility () {
    fabOpen.setOnClickListener (v -> {
        if (buttonClick == 0){
            buttonClick = 2;
            overridePendingTransition (R.anim.fade_in, R.anim.fade_out);
            scanFab.setVisibility (View.VISIBLE);
            historyFab.setVisibility (View.VISIBLE);
        }else if (buttonClick == 2){
            buttonClick = 0;
            overridePendingTransition (R.anim.fade_in, R.anim.fade_out);
            scanFab.setVisibility (View.GONE);
            historyFab.setVisibility (View.GONE);
        }
    });
}