ldPr
ldPr

Reputation: 61

How to add a character before editText?

I need the user to type 11 digits but if he types less, zeros appear before the text typed. Example:

11 digit text: 52396514262

If the user only enters 523: I want the edit text to appear 00000000523

cpf_formulario_abastecimento.addTextChangedListener(object  : TextWatcher{
        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

        }

        override fun afterTextChanged(p0: Editable?) {
            for (i in conexaoAPI.listaMotorista!!){
                if (cpf_formulario_abastecimento.text!!.toString() == i.cpf){
                    runOnUiThread {
                        nomeMotoristaHint.text = i.nomeCompleto
                    }
                }
            }
            
        }

    })

Upvotes: 0

Views: 86

Answers (2)

tomysek
tomysek

Reputation: 189

Change

if (cpf_formulario_abastecimento.text!!.toString() == i.cpf){

to:

if (String.format("%011d", Integer(cpf_formulario_abastecimento.text!!.toString())) == i.cpf){

Upvotes: 1

Abdallah Abdel Aziz
Abdallah Abdel Aziz

Reputation: 676

Simply

int num  = 523;
String formatted = String.format("%011d", num);

Upvotes: 0

Related Questions