OnTouch delay (Android)

I have an app where I need to have a delay after each touch in an ImageButton.

I tried the Thread.sleep() method, but I am not sure if this is the best way to deal with it.

What do you guys recommend?

Any help is appreciatted!

ONE MORE THING: I want the content of the onTouch() event to be fired THEN I want to delay "X" seconds the next onTouch() event. It's like to prevent the user to click too many times in the button.

Answers


Since all touch events are handled by UI thread, Thread.sleep() will block your UI thread which is (I hope) not what you are looking for. I think the most correct way to solve your problem would be using postDelayed(Runnable, long) interface in your onClick handler which allows your to delay execution:

@Override
public void onClick(View v)
{
    postDelayed(new Runnable()
    {

        @Override
        public void run()
        {
            // do your stuff here
        }
    }, 10000); //10sec delay
}

UPDATE:

If you want user to prevent clicking too fast on your image view, I strongly recommend go with onClick rather than onTouch (unless there are serious reasons for that)

However, please see the code snippet which might help you:

private boolean blocked = false;
private Handler handler = new Handler();

@Override
public boolean onTouchEvent(MotionEvent event)
{
    if (event.getAction() == MotionEvent.ACTION_DOWN)
    {
        if (!blocked)
        {
            blocked = true;
            handler.postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    blocked = false;
                }
            }, 1000);
        } else
        {
            return false;
        }
    }
    return super.onTouchEvent(event);
}

Need Your Help

Adding a specific range of numbers to an array

java arrays

I have been looking and looking at my code and I still can't figure out why the result is always the highestGrade value and not a different value for every position in the array.

Website URL: is directing page to a modified index

php apache .htaccess mod-rewrite

Sorry for the awkward title. Here's whats going on.. If i go to:

Backslash '\' in console.log() not appearing

javascript jquery

I'm trying to use a back slash in console.log() and within <p></p> but it seems that when the page loads, all back slashes are removed.

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.