Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – How to wait for a few seconds between text changed in Android?

Posted on November 14, 2022

Solution 1 :

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) 
            myCountDownTimer.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.

READ  [ANSWERED] android - Notification.addAction Not working when the app is backgrounded or closed
Powered by Inline Related Posts

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

Problem :

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?

Comments

Comment posted by Aniket Sahrawat

Thread.sleep(5000)

Comment posted by vesii

@AniketSahrawat that’s not gonna work because

Comment posted by Aniket Sahrawat

That needs to be executed in a different thread and checked for input. But if you aren’t familiar with concurrency then don’t do it because it will create more harm than ease.

Comment posted by vesii

I do understand how threads work. The problem is that

Comment posted by UkFLSUI

Use a

Comment posted by vesii

You are awesome 🙂

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress