Android Motion Event Hold Down Button

I have a seemingly simple question that I need help with. I have a button. I want it so when I click on the button quickly it adds one to a total. I also want it to be so that when I hold down this same button for about 2 seconds, it removes one from the total. The only part I am having trouble with is the motion event part. I have been experimenting with ACTION_UP and ACTION_DOWN with no luck. Is there an easy way to do this?

switch (event.getAction())
       {    
           case MotionEvent.ACTION_DOWN:
           {
               total ++;
               return true;
           }

           case MotionEvent.ACTION_UP:
           {
               if(total >0){
                total--;
               }
               return true;
           }
        }

Thanks for the help!

Answers


Why not use the Button's setOnClickListener and setOnLongClickListener?

The only limitation with this approach is that you cannot set the the timeout value, which is somewhat over 1,5 seconds, if I remember correctly.

If you're persistent about the two seconds (or some other value), then I suppose you could use an onTouchListener and keep track of the pressed time yourself. In that case, the MotionEvent's getDownTime might be of interest to you.


Need Your Help

Attr value is declared in two different libraries

android design attributes styles

I added a library to my project in Android Studio. But as soon as I tried to compile my project I got this error:

Can I pass complex parameters to a Java Applet?

java html parameters applet

I'm really new to Java and I have to create an applet for signing documents electronically. The applet will be called from an ASP.Net web page application.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.