Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7692863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:57:03+00:00 2026-05-31T20:57:03+00:00

I currently have some action associated with when the user types something in a

  • 0

I currently have some action associated with when the user types something in a AutocompleteTextView. However, I only want the action to register if the user stops typing. Essentially, I don’t want the action to happen while the user to spam typing. The following code might explain it a little better.

AutocompleteTextView search = ...;
search.setOnKeyListener(new onKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            if (mTimer != null)
                mTimer.cancel();
            mTimer = new Timer();
            mTimer.schedule(new TimerTask() {
                public void run() {
                    my_func();
                }
            }, 1000);
            return true;
        }
        return false;
    }
}

public void my_func() {
    Looper.prepare();

    // Do some function that changes the View

    Looper.loop();
}

Any ideas how I can force the keyListener to wait a certain number of seconds?

EDIT:

I am now using Timer to schedule the function. Refer to above edited code.

The FIRST timer works. However, when I try to re-evoke the function the second timer, I get an exception that says only main thread that initialized the View can modify the View. I am not sure how Looper works (I just added Looper because it complained before that I need to use it). Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:57:04+00:00Added an answer on May 31, 2026 at 8:57 pm

    Create a Timer. When the Timer fires, do your action. If you receive another keypress before the Timer expires, cancel the timer, and reset it. Something like,

    public boolean onKey(View v, int keyCode, KeyEvent event) {
      if (mTimer != null) {
        mTimer.cancel();
      }
      mTimer = new Timer();
      mTimer.schedule(new TimerTask() {
        @Override 
        public void run() {
          // do what you need to do after the one second here
          mTimer = null;
        }
      }, 1000);
    }    
    

    If you modify the view tree from inside the timer task’s run() method, you must force it to happen on the UI thread. You can do this like,

    YouActivity.this.runOnUiThread(new Runnable() {
      @Override
      public void run() {
        // modify your UI components here
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a table structure that looks something like this(some details omitted): ColumnName
I currently have some code which needs to perform multiple updates per user for
I have 2 classes: truck and sedan. When some action occurs, I want to
I currently have some code that will produce a crash dump when my application
I currently have some code that pulls down a list of users in a
I've been trying to implement unit testing and currently have some code that does
I'm tinkering with Silverlight 2.0. I have some images, which I currently have a
I'm using Zend_Search_Lucene, the PHP port of Java Lucene. I currently have some code
Currently i have some interfaces (stripped down for here): public interface IJobGroup { string
We have some stuff that may be exported into various formats. Currently we have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.