Saturday, 3 April 2021

How to make EditText Mask format in Android

 How to make like below image in android.


This is the called as Edittext mask making , it means that how to make edittext inserting data format like card view , like expiry date format etc...

So here we learn how to make Edittext Mask Making Format in android.


edtmask.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
String str = s.toString();
//Here s.length() == 4 means after entering 4 digit give 1 space Ex. 1234
// Here s.length() == 9 means after entering 9 digits (it involve space also) add 1 space Ex. 1234 5789
// Here s.length() == 14 means last number is 14 digit Ex. 1234 5789 1198 1234
// This is how this Edittext Mask works
// Here you can also add Alphabets or Mix number and alphabets also.
if (s.length() == 4 || s.length() == 9 || s.length() == 14) {
str += " ";
edtmask.setText(str);
edtmask.setSelection(str.length());
}
} catch (Exception ignored) {
}
}
@Override
public void afterTextChanged(Editable s) {
}
});


Here is the code for making editext Card data inserting format , for example we have to add
card number like Ex. 1112 4444 4444 5555 
so, above code for making like this. 

Source code for EditText Mask

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...