Improved solution from so answer, with copy-paste support
Credit card input filter example:
yourEditText.setFilters(new InputFilter[]{
new InputFilter.LengthFilter(23),
new SpacesInputFilter(23, new int[]{4, 9, 14, 19}, ' '),
});