Created
April 29, 2019 05:01
-
-
Save piyush-malaviya/050fa60552f65025bdf542ad1414c090 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Handler; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
public abstract class DelayTextWatcher implements TextWatcher { | |
private final long DELAY = 300; // milliseconds | |
private Handler handler = new Handler(); | |
private Runnable runnable = new Runnable() { | |
@Override | |
public void run() { | |
onAfterTextChanged(); | |
} | |
}; | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
handler.removeCallbacks(runnable); | |
handler.postDelayed(runnable, DELAY); | |
} | |
protected abstract void onAfterTextChanged(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Handler; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
public abstract class TypingTextWatcher implements TextWatcher { | |
private final long DELAY = 1000; // milliseconds | |
private Handler handler = new Handler(); | |
private boolean isTyping = false; | |
private Runnable runnable = new Runnable() { | |
@Override | |
public void run() { | |
isTyping = false; | |
onTyping(false); | |
} | |
}; | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
if (!isTyping) { | |
onTyping(true); | |
isTyping = true; | |
} | |
handler.removeCallbacks(runnable); | |
handler.postDelayed(runnable, DELAY); | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
} | |
protected abstract void onTyping(boolean isTyping); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment