Android - I want to get a number input from the user into an EditText - it needs to be separated by spaces - every 4 characters. Example: 123456781234 -> 1234 5678 1234
change the live text while typing is some what difficult. we should handle the following issues.
a. cursor position b. we should allow the user delete the entered text.
The following code handle both the issues.
Add TextWatcher to EditText, and get the text from "afterTextchanged()" and write your logic
String str=""; int strOldlen=0;
@Override
public void afterTextChanged(Editable s) {
str = edtAadharNumber.getText().toString();
int strLen = str.length();
if(strOldlen<strLen) {
if (strLen > 0) {
if (strLen == 4 || strLen == 9) {
str=str+" ";
edtAadharNumber.setText(str);
edtAadharNumber.setSelection(edtAadharNumber.getText().length());
}else{
if(strLen==5){
if(!str.contains(" ")){
String tempStr=str.substring(0,strLen-1);
tempStr +=" "+str.substring(strLen-1,strLen);
edtAadharNumber.setText(tempStr);
edtAadharNumber.setSelection(edtAadharNumber.getText().length());
}
}
if(strLen==10){
if(str.lastIndexOf(" ")!=9){
String tempStr=str.substring(0,strLen-1);
tempStr +=" "+str.substring(strLen-1,strLen);
edtAadharNumber.setText(tempStr);
edtAadharNumber.setSelection(edtAadharNumber.getText().length());
}
}
strOldlen = strLen;
}
}else{
return;
}
}else{
strOldlen = strLen;
Log.i("MainActivity ","keyDel is Pressed ::: strLen : "+strLen+"\n old Str Len : "+strOldlen);
}
}
}
Here I am trying to add space for every four characters. After adding first space, then the length of the text is 5. so next space is after 9 characters like that.
if (strLen== 4||strLen==9)
edtAadharNumber.setSelection(edtAadharNumber.getText().length());
is this editext for credit card?
first create count variable
int count = 0;
then put this in your oncreate(activity) / onviewcreated(fragment)
ccEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) { /*Empty*/}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) { /*Empty*/ }
@Override
public void afterTextChanged(Editable s) {
int inputlength = ccEditText.getText().toString().length();
if (count <= inputlength && inputlength == 4 ||
inputlength == 9 || inputlength == 14)){
ccEditText.setText(ccEditText.getText().toString() + " ");
int pos = ccEditText.getText().length();
ccEditText.setSelection(pos);
} else if (count >= inputlength && (inputlength == 4 ||
inputlength == 9 || inputlength == 14)) {
ccEditText.setText(ccEditText.getText().toString()
.substring(0, ccEditText.getText()
.toString().length() - 1));
int pos = ccEditText.getText().length();
ccEditText.setSelection(pos);
}
count = ccEditText.getText().toString().length();
}
});
Here is a little help function. For your example you would call it with
addPadding(" ", "123456781234", 4);
/**
* @brief Insert arbitrary string at regular interval into another string
*
* @param t String to insert every 'num' characters
* @param s String to format
* @param num Group size
* @return
*/
private String addPadding(String t, String s, int num) {
StringBuilder retVal;
if (null == s || 0 >= num) {
throw new IllegalArgumentException("Don't be silly");
}
if (s.length() <= num) {
//String to small, do nothing
return s;
}
retVal = new StringBuilder(s);
for(int i = retVal.length(); i > 0; i -= num){
retVal.insert(i, t);
}
return retVal.toString();
}
as @waqas pointed out, you'll need to use a TextWatcher if your aim is to make this happen as the user types the number. Here is one potential way you could achieve the spaces:
StringBuilder s;
s = new StringBuilder(yourTxtView.getText().toString());
for(int i = 4; i < s.length(); i += 5){
s.insert(i, " ");
}
yourTxtView.setText(s.toString());
Whenever you need to get the String without spaces do this:
String str = yourTxtView.getText().toString().replace(" ", "");
You need to use TextWatcher to achieve visual purpose spaces.
And use any simply split string by space logic to join it back or loop through the entire string per character wise and eliminate (char) 32
from the string
Format of text is 000 000 0000
android edittext textwatcher format phone number like xxx-xxx-xx-xx
public class PhoneNumberTextWatcher implements TextWatcher {
private static final String TAG = PhoneNumberTextWatcher.class
.getSimpleName();
private EditText edTxt;
private boolean isDelete;
public PhoneNumberTextWatcher(EditText edTxtPhone) {
this.edTxt = edTxtPhone;
edTxt.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
isDelete = true;
}
return false;
}
});
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
if (isDelete) {
isDelete = false;
return;
}
String val = s.toString();
String a = "";
String b = "";
String c = "";
if (val != null && val.length() > 0) {
val = val.replace(" ", "");
if (val.length() >= 3) {
a = val.substring(0, 3);
} else if (val.length() < 3) {
a = val.substring(0, val.length());
}
if (val.length() >= 6) {
b = val.substring(3, 6);
c = val.substring(6, val.length());
} else if (val.length() > 3 && val.length() < 6) {
b = val.substring(3, val.length());
}
StringBuffer stringBuffer = new StringBuffer();
if (a != null && a.length() > 0) {
stringBuffer.append(a);
if (a.length() == 3) {
stringBuffer.append(" ");
}
}
if (b != null && b.length() > 0) {
stringBuffer.append(b);
if (b.length() == 3) {
stringBuffer.append(" ");
}
}
if (c != null && c.length() > 0) {
stringBuffer.append(c);
}
edTxt.removeTextChangedListener(this);
edTxt.setText(stringBuffer.toString());
edTxt.setSelection(edTxt.getText().toString().length());
edTxt.addTextChangedListener(this);
} else {
edTxt.removeTextChangedListener(this);
edTxt.setText("");
edTxt.addTextChangedListener(this);
}
}
}