Created one Edit text which accepts only number, when i click on it. It shows numeric keyboard. But my problem is when I copy one emoticons from Whatsapp and paste it into my edit text box it accepts that emoticons. I want a edit text which doesn't allow to accept emoticons after copying from other apps, It should accept only numbers (by typing or by copying) Note: I don't want disable copy paste.
7 Answers
Set your EditText to have inputType number
by doing this in XML
:
android:inputType="number"
or programmatically by:
yourEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
If you are looking to disable copying as well then you should setInputFilter
to the yourEditText
to filter out the non-digit characters.

- 1,750
- 1
- 18
- 30
-
I used it. But It accpets the emoticons from other app. If i copy one emoticons and paste it over edit text it accpets it. – Rohan Zemse Dec 28 '16 at 06:46
Just use the input type as number it will not allow to paste anything.
<EditText
android:id="@+id/edtEnterAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/enter_amount_margin"
android:textSize="@dimen/header_three"
android:singleLine="true"
android:maxLength="8"
android:inputType="numberDecimal"
android:imeOptions="actionDone"
android:hint="Enter amount"
android:textColorHint="@color/gray_two" />
OR
android:inputType="number"
OR
android:digits="0123456789"
OR
edtEnterAmount.setOnFocusChangeListener(this);
InputFilter filter = new InputFilter() {
final int maxDigitsBeforeDecimalPoint=8;
final int maxDigitsAfterDecimalPoint=2;
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
StringBuilder builder = new StringBuilder(dest);
builder.replace(dstart, dend, source
.subSequence(start, end).toString());
if (!builder.toString().matches(
"(([1-9]{1})([0-9]{0,"+(maxDigitsBeforeDecimalPoint-1)+"})?)?(\\.[0-9]{0,"+maxDigitsAfterDecimalPoint+"})?"
)) {
if(source.length()==0)
return dest.subSequence(dstart, dend);
return "";
}
return null;
}
};
edtEnterAmount.setFilters(new InputFilter[] { filter });
Hope this help.Happy coding.

- 1,985
- 1
- 12
- 22
-
@RohanZemse I have used the same code for my application its working.I had also seen the emoticons are not able to paste in the edit text .please let me know if am wrong. – Sagar Gangawane Dec 28 '16 at 06:52
-
You could use the text Change Listener and check for the newly added character.
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
//check if all characters in CharSequence is a number
}
});

- 1,574
- 1
- 24
- 24
You should try to use an InputFilter on yout editText:
public static class NumericInputFilter implements InputFilter {
public CharSequence filter(CharSequence charSequence, int start, int end, Spanned dest, int dstart, int dend) {
StringBuilder builder = new StringBuilder();
for (int index = 0; index < end; index++) {
char character = charSequence.charAt(index);
if (Character.isDigit(character)) {
builder.append(character);
}
}
boolean isAllCharacterValid = (builder.length() == end - start);
return isAllCharacterValid ? null : builder.toString();
}
}
You can set this input filter on your edit text by this way:
editText.setFilters(new InputFilter[]{new NumericInputFilter()});
If you use this input filter, no matter if you copy paste a character or write with keyboard, each character will be filtered, and if it's not a number it will not be shown in your edit text.
Hope it helps!

- 408
- 1
- 4
- 17
-
-
why answer was down voted? Accepted answer based on this one. – Roman Pozdnyakov Feb 21 '17 at 16:50
You can use android:inputType="number"
or android:digits="0123456789"

- 991
- 1
- 13
- 27
-
-
You can override canPaste() and isSuggestionEnabled() by extending edit text to avoid Paste popup on long click. Refer http://stackoverflow.com/questions/27869983/edittext-disable-paste-replace-menu-pop-up-on-text-selection-handler-click-even – seema Dec 28 '16 at 07:02
-
try adding
android:digits="0123456789"
to your xml file.

- 2,584
- 2
- 15
- 28
-
1While this code may answer the question, providing additional [context](https://meta.stackexchange.com/q/114762) regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit](http://stackoverflow.com/posts/41356821/edit) your answer to add an explanation, and give an indication of what limitations and assumptions apply. It also doesn't hurt to mention why this answer is more appropriate than others. – Dev-iL Apr 05 '17 at 07:27
@user3707908
Bang on !!! Its perfectly works for me........ I modified it to accpet "." also.
public static class NumericInputFilter implements InputFilter {
public CharSequence filter(CharSequence charSequence, int start, int end, Spanned dest, int dstart, int dend) {
StringBuilder builder = new StringBuilder();
for (int index = 0; index < end; index++) {
char character = charSequence.charAt(index);
if (Character.isDigit(character)) {
builder.append(character);
} else if(Character.toString(character).equalsIgnoreCase(".")){
builder.append(character);
}
}
boolean isAllCharacterValid = (builder.length() == end - start);
return isAllCharacterValid ? null : builder.toString();
}
}

- 49
- 1
- 9