I have an issue when user try to enter text copied that this text sometimes contains some special characters like �
and this make JSON string to be not formated, so please how can I avoid user enter such characters
please take into consideration that user can enter Arabic text and English text only
Try using InputFilters on the Edittext:
InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = start; i < end; i++) {
if (isEnglishOrArabicChar(source.charAt(i))) {
stringBuilder.append(source.charAt(i));
}
}
return stringBuilder.toString();
}
};
etName.setFilters(new InputFilter[]{filter});
private static boolean isEnglishOrArabicChar(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
return ub == Character.UnicodeBlock.ARABIC || ub==Character.UnicodeBlock.BASIC_LATIN;
}
Reference
I want to restrict my edittext to only accept farsi charecters . charecters like ض ص ث ق ف And so on .
I've tried to use xml digit but it did not work .
how can I do so ?
You can use InputFilter for the EditText. InputFilters can be attached to Editables to constrain the changes that can be made to them.
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
String text = String.valueOf(source.charAt(i));
Pattern RTL_CHARACTERS = Pattern.compile("[\u0600-
\u06FF\u0750-\u077F\u0590-\u05FF\uFE70-\uFEFF]");
Matcher matcher = RTL_CHARACTERS.matcher(text);
if (matcher.find()) {
return ""; // it's Persian
}
}
return null;
}
};
Use this filter to detect Farsi/Persian characters and restrict them for edittext. Set this filter to your EditText using
editText.setFilters(new InputFilter[]{filter});
This will restrict Farsi/Persian characters
These code can help too:
InputFilter nameFilter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isLetterOrDigit(source.charAt(i))) { // if digit!
String blockCharacterSet = getString(R.string.all_fa_words) + ' ';
if (blockCharacterSet.contains(String.valueOf(source.charAt(i)))) {
edtNameEdit.setHintTextColor(getResources().getColor(R.color.materialWhite));
edtNameEdit.setHint(getString(R.string.name_lastname));
return null;
} else {
edtNameEdit.setHintTextColor(getResources().getColor(R.color.sPink));
edtNameEdit.setHint(getString(R.string.name_lastname) + ' ' + getString(R.string.must_fa));
return edtNameEdit.getText().toString();
}
} else {
return null;
}
}
return null;
}
};
edtNameEdit.setFilters(new InputFilter[]{nameFilter});
edtNameEdit.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
This question already has answers here:
How do I use InputFilter to limit characters in an EditText in Android?
(20 answers)
Closed 9 years ago.
How can I do that if the user is not typing A, then there will not be any output? I want to do this in a EditText.
Code:
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.toString(source.charAt(i)).equals("a")) {
return "";
}
}
return null;
}
};
You could do it the way that you are attempting to, with an input filter, but I would recommend that you do it in XML instead, by giving your edittext the attribute android:digits="Aa". That should solve your problem.
If you need to use an input filter try this (it is untested and may not work an better than yours)
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String chars = "";
for (int i = start; i < end; i++) {
if (Character.toString(source.charAt(i)).equals("a")) {
chars = chars + source.charAt(i);
} else {
//don't add anything to the char sequence
}
}
return chars;
}
};
Hi I want to show only numbers and characters on the keypad for EditText in android, I did try to add the attribute android:inputType = text|number but it did not work.
Please help me with any other better suggestion. thanks in advance.
Use the filter for that. Here I am adding the code for filter.
EditText etName = (EditText)findViewById(R.id.etName);
InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
etName.setFilters(new InputFilter[]{filter});
try to add the digits parameter to your editText:
android:digits="abcde.....012345789"
The solution with InputFilter provided here is not 100% correct as it will replace and throw out some valid characters from the input if they are right next to the invalid one.
For example we need to filter out all special characters and you enter text: olala[
The EditText field will pass the whole olala[ sentence to the filter and the return value will be "" meaning that we throw out valid olala as well.
Here is my solution:
InputFilter filter = (source, start, end, dest, dstart, dend)->{
for (int i = start; i < end; i++) {
char symbol = source.charAt(i);
if (!isValidCharacter(symbol)) {
StringBuilder buf = new StringBuilder();
for(int j = start; j < end; j++)
{
symbol = source.charAt(j);
if(isValidCharacter(symbol)) buf.append(symbol);
}
return buf.toString();
}
}
return null;
};
We need double loop here to avoid memory allocation of StringBuilder for every method call with valid characters.
If you want to add spaces you can give space after the last digit.
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 "
try using this method it worked for me:
public static InputFilter[] getFilter(String blockChars) {
return new InputFilter[]{(source, start, end, dest, dstart, dend) -> {
for (int i = start; i < end; i++) {
if (source != null && blockChars.contains("" + source.charAt(i))) {
return source.subSequence(start, i);
}
}
return null;
}};
add this line for your edit text
edittext.setFilters(getFilter("#~"));
The expected solution is to restrict user from entering special characters from keyboard.
The below solution uses RegX but adds it to strings.xml file so that will be taken care at the time of creating multilingual xmls.
Strings.xml
<string name="alpha_numeric_regx">[a-zA-Z 0-9]+</string>
Source file
//Extracting forehand to avoid multiple calls to getString from Filter
String alphaNumericRegX = getString(R.string.alpha_numeric_regx);
mEditTextOtp.setFilters(new InputFilter[]{(source, start, end, dest, dStart, dEnd) -> {
for (int i = 0; i < source.length(); i++) {
if (source.equals("")) {
return source;
}
if (source.toString().matches(alphaNumericRegX)) {
return source;
}
return "";
}
return null;
}});
Hope this will solve the prob. for new guys. :)
Kotlin
et_search.keyListener = DigitsKeyListener.getInstance(getString(R.string.alphanumeric))
String.xml
<string name="alphanumeric">0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ</string>
//大兄弟,这么做就可以了。
InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (isChineseChar(source.charAt(i))) {
return "";
}
}
return null;
}
};
etName.setFilters(new InputFilter[]{filter});
//一条简单的规则。
private static boolean isChineseChar(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS;
}
Its Working
Restrict Special Symbol in Edittext
private EditText your_editText ;
private String blockCharacters = "(~*#^|$%&!";
private InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source != null && blockCharacters.contains(("" + source))) {
return "";
}
return null;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
your_editText = (EditText) findViewById(R.id.your_editText);
your_editText .setFilters(new InputFilter[] { filter });
}
}
I couldn't edit a previous answer, so I did a Kotlin version of it (including whitespace):
private val userNameFilter =
InputFilter { source, start, end, dest, dStart, dEnd ->
for (i in start until end) {
if (!Character.isLetterOrDigit(source[i]) && !Character.isWhitespace(source[i])) {
return#InputFilter ""
}
}
null
}
I am a beginner in Android.
I have an EditText for one letter. It accepts only characters. I need to replace entered character with the new one when the user enters different character on the keyboard. How can I achive that? Is this possible?
// Set edit text width only to one character
InputFilter[] FilterArray = new InputFilter[2];
FilterArray[0] = new InputFilter.LengthFilter(1);
FilterArray[1] = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetter(source.charAt(i))
&& !Character.isSpaceChar(source.charAt(i))) {
return "";
}
}
return null;
}
};
editLetter.setFilters(FilterArray);
Consider using regular expressions and replaceAll as in:
// ASSERT inString not null
public static String removeTags(String inString) throws IllegalArgumentException {
if (inString == null) {throw new IllegalArgumentException();}
return inString.replaceAll("<.*>","");
}
This answer may or may not help you at all.
The TextWatcher should come in handy for this.