After entering 2 numbers insert a colon(:) in android

1.9k Views Asked by At
       edtTxt.addTextChangedListener(new TextWatcher() {

           @Override
           public void afterTextChanged(Editable s) {
                if(s.length() != 0 && s.length() == 2){
                    String str = s.toString();
                    str.replaceAll("..(?!$)", "$0:");
                    edtTxt.setText(str);
                  }
           }

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

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

           }
          });

I need to display ":" after 2nd digit that is for example 10:25, maximum length is 5 digits it is edittext. If i started typing in the edittext 10 after this ":" should be inserted then 10:25 should be displayed in the edittext. I tried with the above logic not working. can anyone help me. Thanks in advance

4

There are 4 best solutions below

0
On

First of all you ignore the result of str.replaceAll(). The method returns a String.

The if condition can be simplified to s.length() == 2.

And the regex you are using doesn't work.

This will add colon in the EditText after you have entered 2 characters

if (s.length() == 2) {
    edtTxt.setText(s.toString() + ":");
}
3
On

After replaceAll you should assign the value to same variable. Its working fine..

public void afterTextChanged(Editable s) {
    if(s.length() != 0 && s.length() == 3){
        String str = s.toString();
        str = str.replaceAll("..(?!$)", "$0:");
        edtTxt.setText(str);
        edtTxt.setSelection(edtTxt.getText().length()); //cursor at last position
     }
}
0
On

Kotlin and improved version of the @sasikumar's solution:

private fun formatInput(clock: Editable?) {
        if (clock.toString().isNotEmpty()
            && clock.toString().contains(":").not()
            && clock.toString().length == 3
        ) {
            var str: String = clock.toString()
            str = str.replace("..(?!$)".toRegex(), "$0:")
            etClock.setText(str)
            etClock.setSelection(etClock.text.length)
        }
    }
etClock.addTextChangedListener(
            afterTextChanged = {
                formatInput(it)
            }
)
etClock.setOnFocusChangeListener { _, hasFocus ->
            if (hasFocus) {
                etClock.setSelection(etClock.text.length)
            }
}
etClock.setOnClickListener {
            etClock.setSelection(etClock.text.length)
        }

And a great explanation of the used Regex: https://stackoverflow.com/a/23404646/421467

0
On

Just do it like this

editTextTime.addTextChangedListener {
        if(it?.length == 3 && !it.contains(":")){
            it.insert(2,":")
        }
    }

I think the code is clear
if you put 3 number it will add ":" before the 3rd number
and it will check if your 3rd Char it not already :
then it will insert ":" for you