Sepehr Gouran
Sepehr Gouran

Reputation: 1

implement my own button listener

Hi I want to implement my own Button Listener Interface on Android. It's between OnClickListener and OnLongClickListener Interfaces.

I create OnMyTouchListener Interface :

public interface OnMyTouchListener {
  public static int duration = 1;
  public void onMyTouch(View view);
}

And I extend my button :

public class Button3D extends Button {
  public Button3D(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public void addMyTouchListener(OnMyTouchListener onMyTouchListener){
      //  ???
  }
}

I want to call onMyTouch method when touch duration is more than 1sec and less than 2sec but could not find the solution.

Upvotes: 0

Views: 69

Answers (1)

Burkhard
Burkhard

Reputation: 14738

Just an idea to get you started:

In Button3D you nead a reference to the OnMyTouchListener (probably a List).
In Button3D.addMyTouchListener you only add the Object to the List.

Now the tricky part:
- when you click on the Button, you save the System.currentNanos()
- when you release the Button, you calculate the elapsed time and compare it to your two values. It it is between, fire the onMyTouch for all Listener.

Upvotes: 2

Related Questions