0

I have created an array of buttons. Now what I want is on double click of each of the buttons I want to display a toast which shows the text of the button which is clicked. I did it for singleClick but I don't know how to do it for doubleClick.

The code I have written for an Array of buttons:

LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
    LinearLayout rowLayout = null;
    
    LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);

    //Create Button
    for (int i = 0; i<6; i++)
    {
        rowLayout = new LinearLayout(this);
        rowLayout.setWeightSum(7);
        layoutVertical.addView(rowLayout, param);   
        
        for(int j=0; j<7; j++)
        {
            m_pBtnDay[i][j] = new Button(this);             
            m_pBtnDay[i][j].setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
            rowLayout.addView(m_pBtnDay[i][j], param);
            
            m_pBtnDay[i][j].setTextSize(12);
            //save button position
            m_pBtnDay[i][j].setTag(new CalendarForm(i , j));
        }
    }
iknow
  • 8,358
  • 12
  • 41
  • 68
AndroidDev
  • 4,521
  • 24
  • 78
  • 126

1 Answers1

0

One way I can think of is by using Gesture Detection

 yourButton.setOnTouchListener(touchListener);

.

 // Gesture detection
    gestureDetector = new GestureDetector(new GestureListener());
    View.OnTouchListener touchListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

// The onTouch method has a View parameter which is a reference to the touched view. Cast 
// this to Button to get its caption:

           String caption=((Button)v).getText();
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    };

.

private class GestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
    // event when double tap occurs
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        float x = e.getX();
        float y = e.getY();

        Log.d("Double Click", "Tapped at: (" + x + "," + y + ")");

        return true;
    }
 }
Reno
  • 33,594
  • 11
  • 89
  • 102