0

Here is what I'm trying to do. I need to add a space between a numeric input and an alphabet input. I'm using the TextWatcher class but I could no get what exactly I want

if the input text is:

APyt04XC2446

I need it as

APyt 04 XC 2446

but i'm getting this:

APyt 0 4 X C 2 4 4 6

This is my TextWatcher class:

public class FormattingTextWatcher implements TextWatcher {

private static final String EMPTY_STRING = "";
private static final String WHITE_SPACE = " ";
private String lastSource = EMPTY_STRING;

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {


}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
    String source = s.toString();

    if (!lastSource.equals(source)) {
        source = source.replace(WHITE_SPACE, EMPTY_STRING);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < source.length(); i++) {
            if ((s.charAt(i)>='a' && s.charAt(i)<='z') || (s.charAt(i)>='A' && s.charAt(i)<='Z')) {
            } else {
                stringBuilder.append(WHITE_SPACE);
            }
            stringBuilder.append(source.charAt(i));
        }
        lastSource = stringBuilder.toString();
        s.replace(0, s.length(), lastSource);
    }
} }

kindly get me out of this... thanks

2

2 Answers 2

0

Try this:

public class FormattingTextWatcher implements TextWatcher {

private static final String EMPTY_STRING = "";
private static final String WHITE_SPACE = " ";
private String lastSource = EMPTY_STRING;

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {


}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
    String source = s.toString();

    int letterFlag = -1;

    if(Character.isLetter(character.charAt(0)){
       letterFlag = 0;
    }
    else{
       letterFlag = 1;
    }       

    for (int i = 1; i < source.length(); i++) {

          if(letterFlag == 1){
                if(Character.isLetter(character.charAt(i)){
                    stringBuilder.append(WHITE_SPACE);
                    letterFlag = 0;
                }
          }
          else{
               if(Character.isDigit(character.charAt(i)){
                    stringBuilder.append(WHITE_SPACE);
                    letterFlag = 1;
               }
          }

          stringBuilder.append(source.charAt(i));

   }


} }
Sign up to request clarification or add additional context in comments.

2 Comments

what is character keyword initialization here
Just import it. It is class present in android studio, use alt+enter to import it.
0

For you

public class FormattingTextWatcher implements TextWatcher {

private static final String EMPTY_STRING = "";
private static final String WHITE_SPACE = " ";
private String lastSource = EMPTY_STRING;

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {


}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
    String source = s.toString();

    if (!lastSource.equals(source)) {
        source = source.replace(WHITE_SPACE, EMPTY_STRING);
        StringBuilder stringBuilder = new StringBuilder();
        if ((str.charAt(0) >= 'a' && str.charAt(0) <= 'z') || (str.charAt(0) >= 'A' && str.charAt(0) <= 'Z'))
            flag = 1;
        else if ((str.charAt(0) >= '0' && str.charAt(0) <= '9'))
            flag = 0;
        for (int i = 0; i < str.length(); i++) {
            if (((str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')) && flag == 0) {
                stringBuilder.append(WHITE_SPACE);
                stringBuilder.append(str.charAt(i));
                flag = 1;
                continue;
            } else if ((str.charAt(i) >= '0' && str.charAt(i) <= '9') && flag == 1) {
                stringBuilder.append(WHITE_SPACE);
                stringBuilder.append(str.charAt(i));
                flag = 0;
                continue;
            }
            stringBuilder.append(str.charAt(i));
        }
        lastSource = stringBuilder.toString();
        lastSource = lastSource.replaceAll("\\s+", " "); // it would remove the extra spaces between characters
        s.replace(0, s.length(), lastSource);
    }
}
}

4 Comments

is adding space to the previous separation too..like this APyt 04 XC 2442. the space between apty and 04 is continuously increasing
@Darshuu Fixed the issue you had. Answer Updated
fixed the spacing issue...but here comes another issue when i press backspace in keypad it's add some garbage alphabets instead of deleting the last entered charater...this is only while removing alphabets but in case of letters it works fine
here is the thing happenning its showing as AP 31 gfffffgggfgfdfggs 5643...when i used back press for the 3 rd word

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.