Home > database >  How do I get the length of a number password for an edit text in kotlin
How do I get the length of a number password for an edit text in kotlin

Time:04-30

I have tried doing a text watcher

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            val requiredLength = 4
            val editTextView: EditText = findViewById(R.id.pinInput)

unsure how to write this line of code to get the length

            val textLength = editTextView.text.length
    
    
            val submitButton: Button = findViewById(R.id.button)
    
            submitButton.setOnClickListener {
                
    
                
                if (textLength != requiredLength) {
                    error()
                } else {
                     success()
                }
    
            }

Should I be using a different view

<EditText
        android:id="@ id/pinInput"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:hint="@string/pin"
        android:maxLength="4"
        android:inputType="numberPassword"
        android:importantForAutofill="no"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Please help, also this is my first time posting content like this

CodePudding user response:

you should get the length when the button is pressed

    submitButton.setOnClickListener {
        val textLength = editTextView.text.length

        if (textLength != requiredLength) {
            error()
        } else {
            success()
        }

    }
  • Related