Monday, September 22, 2014

Android Continuous Long Click Listener

This is about implementing a UI Pattern very common for mobile applications. You have a button which increments the value if you do a simple click on a button. If you long press instead of click, it increments the value in chunks rather than single units. These behaviours can be implemented using standard Android library event listeners like View.OnClickListener and View.OnLongClickListener.

For my app, I was looking for implementation where you keep the button pressed and it continuously keeps updating the value in chunks. Sadly, View.OnLongClickListener doesn't have any such support, so I decided to make a custom listener for this behaviour. You can view the final implementation here -


Some notes on the implementation of this listener -
  1. To support continuous long click listener, I am attaching 2 listeners to the view object. View.OnLongClickListener and View.OnTouchListener
  2. View.OnLongClickListener kicks in when it first detects a long click by user. After executing the default behaviour, it adds a delayed message to a Handler to execute the same code. This way, after a stipulated time the same code kicks in and increments the value again and thus simulating the required behaviour for continuous long click.
  3. View.OnTouchListener is used to detect a MotionEvent.ACTION_UP event, where the user disengages from the screen. On detecting it, any pending messages in the Handler are cleared thus finishing the event.
  4. As an add-on, I stimulate a haptic feedback every time the code is run to give user feedback about the long click action being kicked in.
The code for the above sample can be found on github here -> https://github.com/anagri/AndroidLearnings

The gist of listener implementation is below ->

15 comments:

  1. This is very helpful!

    ReplyDelete
  2. Appreciating the persistence you put into your blog and detailed
    information you provide.


    Android Training in Chennai


    Android Training in Bangalore

    ReplyDelete
  3. Needed to compose one little word yet thanks for the suggestions that you are contributed here.
    Best Online Software Training Institute | Android Training

    ReplyDelete
  4. This was very useful indeed. Thank you!

    ReplyDelete
  5. This blog very useful for me to learn and understand easily.Keep sharing like this.

    Selenium Training in Chennai
    Software Testing Training in Chennai

    ReplyDelete
  6. but when longpress deactivated then to still count is incrementing...

    ReplyDelete
  7. Thank you so much for posting this. I really appreciate your work. Keep it up. Great work!Best Placement company in Hyderabad

    ReplyDelete
  8. excellent......
    https://www.wikitechy.com/resume/apache-solr-resume-sample/
    https://www.wikitechy.com/resume/apache-spark-sample-resume/
    https://www.wikitechy.com/resume/application-developer-resume-samples/
    https://www.wikitechy.com/resume/application-support-engineer-resume-sample/
    https://www.wikitechy.com/resume/asp-dot-net-mvc-developer-resume/
    https://www.wikitechy.com/resume/asp-net-core-developer-resume/
    https://www.wikitechy.com/resume/asp-net-developer-resume-samples/
    https://www.wikitechy.com/resume/assistant-accountant-cv-sample/
    https://www.wikitechy.com/resume/assistant-accountant-resume/
    https://www.wikitechy.com/resume/assistant-accountant-resume-sample/

    ReplyDelete
  9. I enjoyed your blog Thanks for sharing such an informative post. We are also providing the best services click on below links to visit our website.
    Oracle Fusion HCM Training
    Workday Training
    Okta Training
    Palo Alto Training
    Adobe Analytics Training

    ReplyDelete