Search code examples
androidkotlinone-time-password

OTP Input in Kotlin - library android-otpview-pinview


I am using below library in my Android application to provide UI for OTP :

https://github.com/mukeshsolanki/android-otpview-pinview

Successfully, Added dependency as below :

implementation 'com.github.mukeshsolanki:android-otpview-pinview:2.1.0'

Also, added below line :

maven { url "https://jitpack.io" }

Using it in layout as below :

       <com.mukesh.OtpView
        android:id="@+id/otp_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:inputType="number"
        android:itemBackground="@drawable/drawable_otp_entry"
        android:textColor="@android:color/white"
        app:itemCount="4"
        app:lineColor="@color/colorPrimary"
        app:viewType="none" />

Now, at the kotlin side in my activity am doing like this :

class MainActivity : AppCompatActivity() {
var otpView: OtpView? =null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    otpView = findViewById(R.id.otp_view)
    otpView.setListener(object:OnOtpCompletionListener() {
        override fun onOtpCompleted(otp:String) {
            // do Stuff
            Log.d("onOtpCompleted=>", otp)
        }
    })
    }
}

Error is at line : otpView.setListener(object:OnOtpCompletionListener() { saying Unresolved reference : setListener and The class does not have a Constructor

am using kotlin and library is might in Java.

What might be the issue?


Solution

  • SetListener is not supported anymore i guess, try using

     otpView = findViewById(R.id.otp_view)
        otpView!!.setOtpCompletionListener(object :OnOtpCompletionListener
        {
            override fun onOtpCompleted(otp: String?) {
    
            }
    
        })