androidandroid-edittextclipboardmanager

Pasting only numbers from the Clipboard


InputType of EditText in my application is number, only. How can I paste only numbers from clipboard to this EditText, if text in clipboard contains both numbers and letter?


Solution

  • Solution: There are 2 things which came up.

    Firstly: As I've tested your question, if you have set EditText as android:inputType="number" then it behaves exactly how you want. If you paste an alphanumeric string then it shows only number. It doesn't show Alphabets or any special characters at all. This was tested in my device Android 7.1.1 (API25).

    Secondly: If You still want to use a workaround which suits your need then you can use TextWatcher:

    I hope this will help. If you have any doubts, please comment below.