Since my question asking about separating characters that are typed in an EditText into groups or blocks didn't receive much attention, I've come here with another question on an alternative way to accomplish what I need.
So, my idea is to have four EditText that have 4 char length. Now what I want is to change the focus as the user type, from an EdutText to another when the limit is reached. Example: Let's say we have EditText A, B, C, D; the first to gain focus is EditText A, then when the chars length at A reach 4, the focus passes to EditText B and so on and stops At D.
I'm trying something like the code bellow, but the focus is not requested when the length reaches 4 chars.
inputFieldA.requestFocus();
inputFieldA.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
inputFieldB.requestFocus();
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputFieldB.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
inputFieldC.requestFocus();
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputFieldC.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
inputFieldD.requestFocus();
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputFieldD.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
//send all to Main EditText (A + B + C + D)
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
So I'm here asking if there is a way to do that.
replace this lines in onTextChange() methode.
instead of adding validation on the basis of CharSeq length add validation on the basis of edittext text like this:
replace
with
Note: update your condition as well.
currEdittext
will be eitherinputFieldA
,inputFieldB
,inputFieldC
depending on your requirementWrite your conditions in afterTextChanged method and make changes as follows :