Home > Software engineering >  toast() isn't working in my code when I am using counter in the project
toast() isn't working in my code when I am using counter in the project

Time:12-28

this are my imports

package com.example.wardrobemeta

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import com.chaos.view.PinView
import org.jetbrains.anko.toast

this are rest of the codes

class OtpActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_otp)
        startTimeCounter()
        val otp = findViewById<PinView>(R.id.Otp)
        otp.setAnimationEnable(true)
        otp.setItemBackgroundColor(getColor(R.color.whitish))
        otp.addTextChangedListener {
            if(otp.text.toString().isNotEmpty()){
                otp.setLineColor(getColor(R.color.Pink))
            }
            if(otp.text.toString().length == 6){
                if (otp.text.toString() == "123456"){
                    toast("otp matched") //this toast isn't working
                }
                else{
                    otp.setLineColor(getColor(R.color.red))//this works which means the if statement is not affected 
                    toast("Invalid OTP") // this too doesn't work
                    otp.text = null

                }
            }

        }

        val resendButton = findViewById<TextView>(R.id.ResendTextView)
        resendButton.setOnClickListener {
            resendButton.isEnabled = false
            startTimeCounter()
        }

    }

this is the funtion for conting time:

    fun startTimeCounter() {
        object : CountDownTimer(30000, 1000) {
            val resend = findViewById<TextView>(R.id.ResendTextView)

            override fun onTick(millisUntilFinished: Long) {
                resend.setTextColor(getColor(R.color.Light_Grey))
                resend.text = "Resend OTP: "   millisUntilFinished / 1000
            }

            override fun onFinish() {
                resend.text = getString(R.string.resend_otp)
                resend.setTextColor(getColor(R.color.blackish))
                resend.isEnabled = true
            }
        }.start()
    }

enter image description here

I am using Material compact theme in sdk 29 and tried running on a device with 30 I would like to know if I need to modify it

CodePudding user response:

Since Anko is now deprecated I would suggest using the regular android way of showing the toast like -

Toast.makeText(OtpActivity.this, "Your message", Toast.LENGTH_LONG).show()

CodePudding user response:

you can simply write in activity Toast.makeText(this,"Your message",Toast.LENGTH_SHORT).show()

in fragment Toast.makeText(requireContext(),"Your message",Toast.LENGTH_SHORT).show()

  • Related