mukeshsolanki / android-otpview-pinview

A custom view to enter otp of different sizes used usually in cases of authentication.
MIT License
707 stars 207 forks source link

OtpView not getting focus #111

Closed slavzilla closed 2 years ago

slavzilla commented 4 years ago

Subject of the issue

OtpView is not getting focus. I have been using it in several other projects and it worked properly. I've added style to my AppTheme but it is still not functioning correctly. When I click several times on it, sometimes but not always, it manages to get focus but even then the keyboard does not show. Most of the time it stays dead for good.

Steps to reproduce

I will leave my XML here just so you can see:

    <eightbitlab.com.blurview.BlurView
        android:id="@+id/verificationFragmentBlurView"
        android:layout_width="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="24dp"
        android:background="@drawable/blur_background"
        app:layout_constraintTop_toBottomOf="@id/verificationFragmentPhoneNumberTextView"
        android:layout_height="wrap_content">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.mukesh.OtpView
                android:inputType="number"
                android:id="@+id/otpView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:itemSpacing="24dp"
                app:itemCount="4"
                android:textSize="32sp"
                android:fontFamily="@font/open_sans_semibold"
                app:itemWidth="48dp"
                android:focusable="true"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginBottom="32dp"
                android:layout_marginTop="24dp"
                android:layout_marginStart="40dp"
                app:lineWidth="2dp"
                app:lineColor="@color/winter40"
                app:state_filled="true"
                app:viewType="line"
                android:layout_marginEnd="40dp"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </eightbitlab.com.blurview.BlurView>
    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="otpViewStyle">@style/OtpWidget.OtpView</item>
    </style>

Expected behaviour

Tell us what should happen

Actual behaviour

I can say that I've tried changing this BlurView to ConstraintLayout and it didn't help. I also tried calling requestFocus() it gets focus but the keyboard won't popup.

slavzilla commented 4 years ago

Oh and by the way, adding focusable in XML didn't help at all.

waihanko commented 4 years ago

https://github.com/mukeshsolanki/android-otpview-pinview/issues/90#issuecomment-593440542

waihanko commented 4 years ago

Oh and by the way, adding focusable in XML didn't help at all.

android:focusableInTouchMode="true" pretty work for me

slavzilla commented 3 years ago

@waihanko but it doesn't for me. @mukeshsolanki buddy, are you going to fix this? I am sad that I can't help but I need your answer. I rely on your view.

mukeshsolanki commented 3 years ago

Can you provide me a sample project with the issue the issue is not reproduce-able on my end

slavzilla commented 3 years ago

Sorry, but I can't. This is the corporate code I am using and it is not open source. This library works very well, it is nice and I grew to love it but it simply doesn't function as it should. I don't know how to help you but if I can I would be glad to try. I just need this working as I am going to spend hours and hours making it myself.

slavzilla commented 3 years ago

What actually works for me is this. I call show keyboard function

fun Context.showKeyboard(
    view: View?
) {
    val imm =
        this.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInputFromWindow(view?.windowToken, 0, 0)
    view?.clearFocus()
}

And then just after that, I do: otpView.requestFocus()

And it works!

slavzilla commented 3 years ago

The other good thing to do is this:

        KeyboardVisibilityEvent.setEventListener(
            requireActivity(),
            viewLifecycleOwner,
            object : KeyboardVisibilityEventListener {
                override fun onVisibilityChanged(isOpen: Boolean) {
                    isKeyboardVisible = isOpen
                }
            })

Where this listener is part of the library https://github.com/yshrsmz/KeyboardVisibilityEvent

And then:

        otpView.setOnClickListener {
            if (!isKeyboardVisible){
                context?.showKeyboard(otpView)
                otpView.requestFocus()
            }
        }

And this ensures that keyboard gets shown on click if the keyboard is not already opened :)

n0dz commented 3 years ago

this worked for me

added following lines of code in onCodeSent() InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); binding.otpView.requestFocus();

and in xml set android:focusableInTouchMode="true"

lsuski commented 3 years ago

the same for me (Android 10) adding android:focusableInTouchMode="true" fixes this issue

absolute-vijju commented 2 years ago

1. Add this property (android:windowSoftInputMode="stateVisible") into your manifest's activity tag like this:

<activity android:name=".ui.AuthActivity" android:exported="true" android:screenOrientation="portrait" android:windowSoftInputMode="stateVisible" />

2. Add this line into your Activity class when you want to open keyboard:

mBinding.otpView.requestFocus()

That's all you need to do :)

mukeshsolanki commented 2 years ago

Please try the latest version of the library