Disable specific keys on the keyboard - android

As you can disable certain keys on the keyboard to prevent the user to input wrong characters?

the following code is to disable numbers and chars
{
final 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.isDigit(source.charAt(i))||Character.isLetter(source.charAt(i)))
{
return "";
}
}
return null;
}
};
answerTXT.setFilters(new InputFilter[]{filter});
}

Related

android- restrict edittext to accept farsi characters only

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);

Restrict special characters & space programmatically

I want to restrict user from entering special characters & space programmatically.
Below is my code
InputFilter[] alphaNumericFilter = new InputFilter[2];
alphaNumericFilter[0] = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int k = start; k < end; k++) {
boolean isLetterOrDigit = Character.isLetterOrDigit(source.charAt(k));
boolean isSpaceChar = Character.isSpaceChar(source.charAt(k));
if ((source.length() > 0 && isSpaceChar) ||
(!isLetterOrDigit && !isSpaceChar) ||
(isSpaceChar && TextUtils.isEmpty(dest))) {
return "";
}
}
return null;
}
};
on pressing space it deleting last character.
Use this code to filter your edittext
private String yourCharacterThatYouWantToBlock= " ~#^|$%&*!";
private InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source != null && yourCharacterThatYouWantToBlock.contains(("" + source))) {
return "";
}
return null;
}
};
Now apply this filter to your edittext.
yourEditText.setFilters(new InputFilter[] { filter });
You can do this way:
public static InputFilter getAlphaNumericInputFilter(){
return new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if(source.equals("")){ // for backspace
return source;
}
if(source.toString().matches("[a-zA-Z0-9 ]+")){
return source;
}
return "";
}
};
}
Here If you can just changes matches string according to your requirement for eg. avoid space with alphanumeric then It should like this:[a-zA-Z0-9]+.
Hope It help you !
InputFilter alphaNumericFilter = new InputFilter() {
#Override
public CharSequence filter(CharSequence arg0, int arg1, int arg2, Spanned arg3, int arg4, int arg5)
{
for (int k = arg1; k < arg2; k++) {
if (!Character.isLetterOrDigit(arg0.charAt(k))) {
return "";
}
}
return null;
}
};
mFirstName.setFilters(new InputFilter[]{ alphaNumericFilter});
Finally I have resolved this issue by adding below line in the code
mEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
This line will not show suggestions on keyboard.

Set InputFilter for white space and maximum length

I want to use Input filter where I can prevent first space entering AND max length of my edit text limited to 200 characters.
so far I have this:
Hot to I put the maxlength into the same filter
private void setInputFilterForEmailAndPwd(final EditText amountEditText) {
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.isSpace(source.charAt(i))) {
return "";
}
}
return null;
}
};
amountEditText.setFilters(new InputFilter[] { filter });
}
setInputFilterForEmailAndPwd(emailEdit);
I found the solution
I put new Input Filter, instead of setInputFilterForEmailAndPwd:
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.isWhitespace(source.charAt(i))) {
return "";
}
}
return null;
}
};
and then:
emailEdit.setFilters(new InputFilter[] { filter, new InputFilter.LengthFilter(200) });

disable white spaces using inputfilter - android

I am a beginner in android and I would like to disable white spaces in edittext using inputfilter.
How can I do that?
I found this code in the net:
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.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
but it allows only letters and digits.
Thank you
Your code in a modified version:
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.isLetter(source.charAt(i)) && !Character.isSpaceChar(source.charAt(i)))
return "";
return null;
}
};
myTextView.setFilters(new InputFilter[] { filter });
Code from SO-Thread Link

Block input if it's not the letter "A" [duplicate]

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;
}
};

Categories

Resources