vesii
vesii

Reputation: 3128

How to wait for a few seconds between text changed in Android?

I'm using MaterialChipsInput (although it does not matter what is the component I'm using). I have the following code:

chipsInput.addChipsListener(new ChipsInput.ChipsListener() {
    @Override
    public void onChipAdded(ChipInterface chip, int newSize) {}

    @Override
    public void onChipRemoved(ChipInterface chip, int newSize) {}

    @Override
    public void onTextChanged(CharSequence text) {
        if (text != null && text.toString().contains(",") && text.toString().length() > 1) {
            final String tag = Utils.capitalizeFully(text.toString().replaceAll(",","").trim());
            if (!(tag.isEmpty())) {
                chipsInput.addChip(tag, null);
            }
        }
    }
});

Basically, it add tags to chipsInput every time user enters a comma. The problem is that the user will have to end with a comma in order to add it as a chip. I would like to add a timer for 5 seconds and if there is no changes after those 5 seconds, it will add that chip to the chipsInput. What would be the easiest way to do it?

Upvotes: 0

Views: 261

Answers (1)

Lalit Fauzdar
Lalit Fauzdar

Reputation: 6363

Its onTextChanged is called from onTextChanged of the Chip's EditText as declared here in the library to which you cannot do much. So, here's I'm pointing out a solution using CountDownTimer.

Why a CountDownTimer? Because You can reset the time back to 0 if user types again under 5 seconds. You can also do it using a Handler and reset it again as done in this answer.

  1. Declare mCountDownTimer a global variable as :

    CountDownTimer mCountDownTimer;
    
  2. Declare countDownFunction() as :

    private void countDownFunction(String chipText) {
        if(mCountDownTimer != null) 
            mCountDownTimer.cancel(); // Cancels the CountDown
    
        mCountDownTimer = new CountDownTimer(5000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //Called after every delay of the above second parameter as a ticking count down
                //Current delay is 1 second as 1000 passed above
            }
    
            @Override
            public void onFinish() {
                //This will only be called on successful five second delay
                chipsInput.addChip(chipText, null); //chipText is the parameter passed to this function
                //You may want to clear the Chip `EditText` here
            }
        };
        mCountDownTimer.start(); // Restarts the CountDown
     }
    
  3. Call your countDownFunction() from the onTextChanged and pass the text to it as :

     @Override
     public void onTextChanged(CharSequence text) {
         if (text != null && text.toString().length() > 1) {
             if(text.toString().contains(",")){
                 final String tag = Utils.capitalizeFully(text.toString().replaceAll(",","").trim());
                 if (!(tag.isEmpty())) {
                     chipsInput.addChip(tag, null);
                 }
             }
             else
                 countDownFunction(Utils.capitalizeFully(text.toString().trim()));
         }
     }
    

Now, to improve this, you can use AsyncTask or Handler with runnable thread. There are many ways to do this, there are many ways to handle the async operation to ensure smooth running app.

But, this gives you the idea - What's happening here is every time a text is entered without comma ,, this function is called and it either starts the count down if not previously started or restarts it because of which the onFinish() of the count down will only be called if it passes the 5 second time and then it will add the chip.

Edit : Now, the resetting of the timer will work better. You can still look at this question.

Upvotes: 2

Related Questions