GoogleMac GoogleMac - 1 year ago 80
Android Question

How can I set an Input Filter to accept only BINARY characters (0 or 1) on Android?

Here is my current code which only allows integers. I want to restrict the user even more to only allow 0s and 1s. I wish it were as simple as

, but it isn't. How can I manipulate the start and end parameters to accomplish this?

final InputFilter binaryOnlyFilter = 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.isDigit(source.charAt(i)) && source.charAt(i) != ' ') {
return "";
return null;

Answer Source

1) If you just want 0's and 1's , add android:digits="01" in your edit text

2) If you want to add some space as well, then try this answer

in that answer, just replace

if(cs.toString().matches("[a-zA-Z ]+")){ 


if(cs.toString().matches("[01 ]+")){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download