Site icon Android Tutorial Online

An OTP Box implementation for case when a single digit should be entered Individually

OtpView

Note: Help Needed in Maintainance. Please raise PR if you think some changes are needed and i will merge them.

An OTP Box implementation for case when a single digit should be entered Individually.

Installation in your Project

Step 1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Step 2. Add the dependency

dependencies {
        implementation 'com.github.aabhasr1:OtpView:v1.1.2-ktx' // kotlin
}

or

dependencies {
        implementation 'com.github.aabhasr1:OtpView:v1.1.2'
}

How to use the library?

Just add the following to your xml design to show the otpview

.....
<in.aabhasjindal.otptextview.OtpTextView
	android:id="@+id/otp_view"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:textColor="#ffffff"
	app:height="40dp"
	app:width="40dp"
	app:bar_enabled="true"
	app:bar_height="2dp"
	app:length="4"
	app:otp="1234"
	app:otp_text_size="24dp"/>
.....

To get a callback when the user enters the otp make use of OTPListener Interface

private OtpTextView otpTextView;
otpTextView = findViewById(R.id.otp_view);
otpTextView.setOtpListener(new OTPListener() {
	@Override
	public void onInteractionListener() {
	// fired when user types something in the Otpbox
	}
	@Override
	public void onOTPComplete(String otp) {
	// fired when user has entered the OTP fully.
	    Toast.makeText(MainActivity.this, "The OTP is " + otp,  Toast.LENGTH_SHORT).show();
    }
});

you also get some additional methods like :

otpTextView.getOtpListener();  // retrieves the current OTPListener (null if nothing is set)
otpTextView.requestFocusOTP();	//sets the focus to OTP box (does not open the keyboard)
otpTextView.setOTP(otpString);	// sets the entered otpString in the Otp box (for case when otp is retreived from SMS)
otpTextView.getOTP();	// retrieves the OTP entered by user (works for partial otp input too)
otpTextView.showSuccess();	// shows the success state to the user (can be set a bar color or drawable)
otpTextView.showError();	// shows the success state to the user (can be set a bar color or drawable)
otpTextView.resetState();	// brings the views back to default state (the state it was at input)

Thats all for now but new additions will be made frequently.

OtpView Attributes

AttributeUse
android:textColorsets the color of the otp text
app:otpsets the otp in the otp view
app:lengthsets the no of otp box in the otp view
app:otp_text_sizesets the otp text size in the otp view
app:text_typefacesets the otp text typeface in the otp view
app:hide_otpsets if the otp entered is to be shown to the user
app:hide_otp_drawablereplaces the pin bullet which is shown to the user when hide_otp is enabled
app:heightsets the height of each box inside the otp view
app:widthsets the width of each box inside the otp view
app:box_marginsets the space between each box in otp view
app:box_margin_leftsets the left space between each box in otp view
app:box_margin_rightsets the right space between each box in otp view
app:box_margin_topsets the top space of each box in otp view
app:box_margin_bottomsets the bottom space of each box in otp view
app:bar_enabledshows a bar below each otp box to the user
app:bar_heightsets the bar height
app:bar_marginsets the bar margin within each box in otp view
app:bar_margin_leftsets the bar left margin within each box in otp view
app:bar_margin_rightsets the bar right margin within each box in otp view
app:bar_margin_topsets the bar top margin within each box in otp view
app:bar_margin_bottomsets the bar bottom margin within each box in otp view
app:bar_active_colorsets the bar color when the cursor is on the box in otp view
app:bar_inactive_colorsets the bar color when the cursor is not on the box in otp view
app:bar_error_colorsets the bar color for error state in otp view
app:bar_success_colorsets the bar color for success state in otp view
app:otp_box_backgroundsets the box background in otp view
app:otp_box_background_activesets the box background when the cursor is on the box in otp view
app:otp_box_background_inactivesets the box background when the cursor is not on the box in otp view
app:otp_box_background_errorsets the box background for error state in otp view
app:otp_box_background_successsets the box background for success state in otp view

Author

Maintained by Aabhas Jindal

Contribution

License

MIT License

Copyright (c) 2019 Aabhas Jindal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

GitHub

View Github

Exit mobile version