Created
August 17, 2016 09:40
-
-
Save joaocsousa/7bbbd95b5e49e8284b9fbcf7e422c23c to your computer and use it in GitHub Desktop.
Android EditText that automatically formats a UK sort code as the user types it in.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.support.v7.widget.AppCompatEditText; | |
import android.text.Editable; | |
import android.text.InputFilter; | |
import android.text.InputType; | |
import android.text.Spanned; | |
import android.text.TextWatcher; | |
import android.text.method.DigitsKeyListener; | |
import android.util.AttributeSet; | |
public class SortCodeView extends AppCompatEditText { | |
{ | |
setHint(getContext().getString(R.string.sort_code)); | |
setInputType(InputType.TYPE_CLASS_NUMBER); | |
setKeyListener(DigitsKeyListener.getInstance("0123456789-")); | |
// this filter filters for characters that will destroy the pattern and either | |
// accepts them or rejects them | |
setFilters(new InputFilter[]{ | |
new InputFilter() { | |
@Override | |
public CharSequence filter(CharSequence source, int start, int end, | |
Spanned destination, int destinationStart, int destinationEnd) { | |
CharSequence result = null; | |
if (end > start) { | |
// adding | |
String destinationString = destination.toString(); | |
String resultingTxt = destinationString.substring(0, destinationStart) + | |
source.subSequence(start, end) + | |
destinationString.substring(destinationEnd); | |
if (!resultingTxt.matches("^(\\d{1,2})(-\\d{0,2}|-\\d{2}-\\d{0,2})?$")) { | |
result = ""; | |
} | |
} | |
// return null to accept the input or empty to reject it | |
return result; | |
} | |
}}); | |
addTextChangedListener(new TextWatcher() { | |
boolean mAdding = false; | |
boolean mRemovedDash = false; | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
if (count > after && s.length() > 0 && s.charAt(start) == '-') { | |
mRemovedDash = true; | |
} | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
mAdding = count > before; | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
if (mAdding) { | |
// let's add a separator every two characters automatically | |
String text = s.toString(); | |
String[] pieces = text.split("\\-"); | |
String lastPiece = pieces[pieces.length - 1]; | |
if (lastPiece.length() == 2 && s.charAt(s.length() - 1) != '-') { | |
s.append('-'); | |
} | |
} else if (mRemovedDash) { | |
removeTextChangedListener(this); | |
// remove the next number automatically after a dash is removed | |
s.delete(s.length() - 1, s.length()); | |
mRemovedDash = false; | |
addTextChangedListener(this); | |
} | |
} | |
}); | |
} | |
public SortCodeView(Context context) { | |
super(context); | |
} | |
public SortCodeView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public SortCodeView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment