OTP input field layout

Hi everyone :partying_face:

I recently helped a friend create an OTP Input Field, and while helping another user with a similar project, I checked online to see if there was something like it.
Since I didn’t find anything,

I’m not sure if “OTP Input Field” this is the correct name for this layout, if you know a better name, let me know, thanks.

I thought it would be nice to make a guide, so let’s get started!

Layout


I created 6 textboxes (but you can use as many as you want) and placed them inside 6 cardviews, giving them a border just to make the layout a bit more lively :grin:.
I also added a button to verify the boxes.

Blocks


We create a variable textboxes with all the textbox components.


I used a WhenAnyTextBox.OnTextChanged to manage everything in a single block.

immagine
First, we check that the modified textbox is present in textboxes

immagine
and save two variables, its position in the textboxes list and its text content.

immagine


If text is empty (meaning the user deleted its content), we set the focus to the previous box by selecting the previous index in textboxes (if the position is 1, it requests focus on itself).

immagine


Similarly, if the length of text is = 1, we set the focus to the next textbox.

immagine


Finally, if the length is greater than 1, we reverse text and select the first character (so if a user types into a box that’s already filled, only the last entered character remains).

blocks(35)
We also set the cursor at the end.


With the button, we simply join all the characters from the textboxes and check if it equals something, in this case, “123456”.

GotFocus/LostFocus

If you want to add focus on the current box, you just need to create a variable cardviews with the respective CardViews

and add the two lost and got focus blocks like this.


Unfortunately, the Stroke Width doesn’t work very well, so I haven’t implemented it.

Result

ezgif-245d8454e340f7

AIA

OTPInputField.aia (4.8 KB)

3 Likes

That’s a really nice guide mate! I appreciate you publishing it! By the way, I recently created a free OTP extension that can perform all these functions with more customization:


I’d love for you to try it out and give me your feedback on what needs to be updated! :blush:

2 Likes

Hi securebugs,

When i say

Just so you know, I’m always referring to guides with native components.

Be careful when you say these things :grin: because all my guides are based precisely on the fact that with extensions I never have full control and there is little customization.

I’ll go try it right away :heart_hands:.

2 Likes

Understandable mate!
BDW there’s a major update in version 2 of this extension with maximum customization. You can try it again now! : [FREE] OTPInput Extension: Customizable OTP Boxes with Auto-Hide & Scroll Support