Skip to content

Instantly share code, notes, and snippets.

@0x10FF
Last active April 1, 2023 02:45
Show Gist options
  • Save 0x10FF/5176884e21511676f2020cc0ebe88d20 to your computer and use it in GitHub Desktop.
Save 0x10FF/5176884e21511676f2020cc0ebe88d20 to your computer and use it in GitHub Desktop.
lower casing text input watcher
package com.sample.util.text;
import android.text.Editable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LowerCasingTextWatcher implements android.text.TextWatcher {
/**
* Optimize me
*/
public static final Pattern UPPER_CASE_REGEX = Pattern.compile("[A-Z]");
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
Matcher matcher = UPPER_CASE_REGEX.matcher(s);
while (matcher.find()) {
CharSequence upperCaseRegion = s.subSequence(matcher.start(), matcher.end());
s.replace(matcher.start(), matcher.end(), upperCaseRegion.toString().toLowerCase());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment