0

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.

1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.