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.
-
Declare
mCountDownTimer
a global variable as :CountDownTimer mCountDownTimer;
-
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 }
-
Call your
countDownFunction()
from theonTextChanged
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.
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 🙂