Home > Blockchain >  Validating EditText to having time in kotlin Android
Validating EditText to having time in kotlin Android

Time:02-17

I have a screen which have 2 edit text one edit text shows hour and other shows minute

enter image description here

I have number of validations that needs to be performed on this

hour edit Text

  • check if entered string if of 12 hour format
  • if user enter like 70 show error Tost and convert it ->07
  • if enter some number like 73 show error and remove last char and make it like ->07

minute Edit Text

  • value should be from 0>59

  • if entered 60 make it 00

  • if value entered is 80 ->show error ->convert it to 08

  • if value entered is 89 -> show error -> remove char and show as 08

     beginHour!!.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) {
             if (p0.toString().isNotEmpty()) {
                 if (TextUtils.isDigitsOnly(p0.toString())) {
                     val hour = Integer.parseInt(p0.toString());
                     if (hour > maxHourValue || hour <= 0) {
                         shortToast(errorMessage)
                         beginHour!!.setText(beginHourString)
                     } else {
                         beginHourString = hour.toString()
                     }
                 } else {
                     shortToast(errorMessage)
                     beginHour!!.setText(beginHourString)
                 }
             } else {
                 beginHourString = ""
             }
         }
    
         override fun afterTextChanged(p0: Editable?) {
         }
     })
    
     beginHour!!.setOnFocusChangeListener { _, hasFocus ->
         if (!hasFocus) {
             try {
                 if (beginHour!!.text.isNullOrEmpty()) {
                     beginHour!!.setText("08")
                     beginHourString = "08"
                 }
                 val hour = 
             Integer.parseInt(beginHour!!.text.toString())
    
                if (value in 0..9) {
                   beginHour.setText("0$value")
             } else {
             beginHour.setText(value.toString())
              }
    
    
             } catch (exception: NumberFormatException) {
             }
         }
     }
    

can anyone suggest and guide me how to implement these validations

CodePudding user response:

Use this logic

fun checkMinute(number: Int): Time {

    if (number == 60) return Time.Success("00")

    if (number in 0..59) {
        return Time.Success(number.toString())
    }

    return singleDigitTime(number)
}

fun checkHour(number: Int): Time {

    if (number in 1..12) {
        val df = DecimalFormat("00")
        val formattedString=df.format(number)
        return Time.Success(formattedString)
    }

    return singleDigitTime(number)
}


private fun singleDigitTime(number: Int): Time {
    if (number in 0..9) {
        return Time.Success(number.toString())
    }

    return Time.Error("0"   number.toString().substring(0, 1))
}

sealed class Time(data: String) {
    class Success(var data: String) : Time(data)
    class Error(var data: String) : Time(data)
}

solution with edittext

binding.etHour
            .textChangedStateFlow(700)
            .collect {
                val p0 = it.toString()
                val current = binding.etHour.text.toString().trim()
                if (current.isNotEmpty()){
                    val hour = Integer.parseInt(current);
                    checkHour(hour).let {
                        when (it) {
                            is Time.Error -> {
                                showToast()
                                Timber.d("error ${it.data.toString()}")
                                binding.etHour.updateText(it.data.toString())
                            }
                            is Time.Success -> {
                                Timber.d("success ${it.data.toString()}")
                                binding.etHour.updateText(it.data.toString())
                            }
                        }
                    }
                }

            }
  • Related