Skip to content

Commit 8428154

Browse files
authored
Merge pull request #14 from swapnil1104/interactive_animation
Added Shake animation
2 parents f514e88 + d30317b commit 8428154

File tree

10 files changed

+108
-44
lines changed

10 files changed

+108
-44
lines changed

.idea/gradle.xml

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android-extensions'
3+
apply plugin: 'kotlin-android'
24

35
android {
46
compileSdkVersion 28
@@ -28,4 +30,8 @@ dependencies {
2830
testImplementation 'junit:junit:4.12'
2931
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3032
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
33+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
34+
}
35+
repositories {
36+
mavenCentral()
3137
}
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
11
package com.broooapps.otpedittext;
22

33
import androidx.appcompat.app.AppCompatActivity;
4+
5+
import android.annotation.SuppressLint;
46
import android.os.Bundle;
57
import android.view.View;
6-
import android.widget.EditText;
78
import android.widget.TextView;
8-
import android.widget.Toast;
99

1010
import com.broooapps.otpedittext2.OnCompleteListener;
1111
import com.broooapps.otpedittext2.OtpEditText;
1212

1313
public class MainActivity extends AppCompatActivity {
1414

15+
TextView textDisplay;
16+
OtpEditText otpEditText;
17+
1518
@Override
1619
protected void onCreate(Bundle savedInstanceState) {
1720
super.onCreate(savedInstanceState);
1821
setContentView(R.layout.activity_main);
1922

20-
OtpEditText editText = findViewById(R.id.top);
23+
otpEditText = findViewById(R.id.oev_view);
24+
25+
textDisplay = findViewById(R.id.text_display);
26+
2127

22-
editText.setOnCompleteListener(new OnCompleteListener() {
28+
otpEditText.setOnCompleteListener(new OnCompleteListener() {
29+
@SuppressLint("SetTextI18n")
2330
@Override
2431
public void onComplete(String value) {
25-
Toast.makeText(MainActivity.this, "Completed " + value, Toast.LENGTH_SHORT).show();
32+
textDisplay.setText("Entered Value: " + value);
2633
}
2734
});
2835

36+
2937
}
3038

3139
public void displayText(View view) {
32-
33-
String input = String.valueOf(((EditText) findViewById(R.id.top)).getText());
34-
((TextView) findViewById(R.id.textView)).setText(input);
35-
40+
String otpValue = otpEditText.getOtpValue();
41+
if (otpValue != null) {
42+
textDisplay.setText("Entered Value: " + otpEditText.getOtpValue());
43+
}
3644
}
3745
}

app/src/main/res/layout/activity_main.xml

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,56 @@
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7-
android:background="#444444"
87
tools:context=".MainActivity">
98

109
<TextView
10+
android:id="@+id/textView"
1111
android:layout_width="wrap_content"
1212
android:layout_height="wrap_content"
13-
android:text="Hello World!"
14-
app:layout_constraintBottom_toBottomOf="parent"
15-
app:layout_constraintLeft_toLeftOf="parent"
16-
app:layout_constraintRight_toRightOf="parent"
13+
android:layout_marginStart="16dp"
14+
android:layout_marginTop="16dp"
15+
android:text="Please enter the OTP"
16+
app:layout_constraintStart_toStartOf="parent"
1717
app:layout_constraintTop_toTopOf="parent" />
1818

1919
<com.broooapps.otpedittext2.OtpEditText
20-
android:id="@+id/top"
20+
android:id="@+id/oev_view"
2121
android:layout_width="match_parent"
2222
android:layout_height="wrap_content"
2323
android:clickable="false"
2424
android:cursorVisible="false"
2525
android:digits="0123456789"
2626
android:maxLength="6"
2727
android:padding="32dp"
28-
app:oev_text_color="#dd1212"
28+
android:hint="hi"
2929
android:textSize="30sp"
30-
app:oev_primary_color="#CCFF00"
30+
app:layout_constraintTop_toBottomOf="@+id/textView"
3131
app:oev_box_style="@string/style_rounded_underline"
32-
app:oev_secondary_color="#DADADA"
33-
app:layout_constraintTop_toTopOf="parent"
3432
app:oev_mask_character="*"
35-
app:oev_mask_input="true" />
33+
app:oev_mask_input="true"
34+
app:oev_primary_color="#CCFF00"
35+
app:oev_secondary_color="#DADADA"
36+
app:oev_text_color="#dd1212"
37+
tools:layout_editor_absoluteX="0dp" />
3638

3739
<Button
3840
android:id="@+id/button"
39-
android:layout_width="0dp"
41+
android:layout_width="wrap_content"
4042
android:layout_height="wrap_content"
41-
android:visibility="gone"
4243
android:onClick="displayText"
43-
android:text="Click me"
44+
android:text="Submit"
4445
app:layout_constraintEnd_toEndOf="parent"
46+
app:layout_constraintHorizontal_bias="0.086"
4547
app:layout_constraintStart_toStartOf="parent"
46-
app:layout_constraintTop_toBottomOf="@id/top" />
48+
app:layout_constraintTop_toBottomOf="@id/oev_view" />
4749

4850
<TextView
49-
android:id="@+id/textView"
51+
android:id="@+id/text_display"
5052
android:layout_width="wrap_content"
5153
android:layout_height="wrap_content"
52-
android:layout_marginTop="80dp"
54+
android:layout_marginTop="15dp"
5355
app:layout_constraintEnd_toEndOf="parent"
54-
app:layout_constraintStart_toStartOf="parent"
55-
app:layout_constraintTop_toBottomOf="@id/button" />
56-
56+
app:layout_constraintStart_toEndOf="@+id/button"
57+
app:layout_constraintTop_toBottomOf="@+id/oev_view" />
5758

5859
</androidx.constraintlayout.widget.ConstraintLayout>

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4+
ext.kotlin_version = '1.3.72'
45
repositories {
56
google()
67
jcenter()
78

89
}
910
dependencies {
1011
classpath 'com.android.tools.build:gradle:3.4.0'
11-
12+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
13+
1214
// NOTE: Do not place your application dependencies here; they belong
1315
// in the individual module build.gradle files
1416
}

images/err_anim.gif

58.7 KB
Loading

otpedittext2/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android-extensions'
3+
apply plugin: 'kotlin-android'
24

35
android {
46
compileSdkVersion 28
@@ -30,4 +32,8 @@ dependencies {
3032
testImplementation 'junit:junit:4.12'
3133
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3234
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
35+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
36+
}
37+
repositories {
38+
mavenCentral()
3339
}

otpedittext2/src/main/java/com/broooapps/otpedittext2/OtpEditText.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* Created by Swapnil Tiwari on 2019-05-07.
55
* swapniltiwari775@gmail.com
66
*/
7-
87
import android.content.Context;
98
import android.content.res.TypedArray;
109
import android.graphics.Canvas;
1110
import android.graphics.Paint;
11+
12+
import androidx.annotation.Nullable;
1213
import androidx.core.content.ContextCompat;
1314
import androidx.appcompat.widget.AppCompatEditText;
1415
import android.text.Editable;
@@ -18,12 +19,12 @@
1819
import android.view.Menu;
1920
import android.view.MenuItem;
2021
import android.view.View;
21-
22+
import android.view.animation.AnimationUtils;
2223

2324
public class OtpEditText extends AppCompatEditText implements TextWatcher {
2425
public static final String XML_NAMESPACE_ANDROID = "http://schemas.android.com/apk/res/android";
2526

26-
private OnClickListener mClickListener;
27+
private View.OnClickListener mClickListener;
2728

2829
private Paint mLinesPaint;
2930
private Paint mStrokePaint;
@@ -69,9 +70,11 @@ public OtpEditText(Context context, AttributeSet attrs, int defStyleAttr) {
6970
init(context, attrs);
7071
}
7172

72-
private void init(Context context, AttributeSet attrs) {
73+
private void init(Context context, @Nullable AttributeSet attrs) {
7374

74-
getAttrsFromTypedArray(attrs);
75+
if (attrs != null) {
76+
getAttrsFromTypedArray(attrs);
77+
}
7578

7679
mTextPaint = getPaint();
7780
mTextPaint.setColor(mTextColor);
@@ -168,9 +171,14 @@ private void getAttrsFromTypedArray(AttributeSet attributeSet) {
168171
a.recycle();
169172
}
170173

171-
@Override
172-
public void setOnClickListener(OnClickListener l) {
173-
mClickListener = l;
174+
@Nullable
175+
public String getOtpValue() {
176+
if (String.valueOf(getText()).length() != mMaxLength) {
177+
this.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake));
178+
return null;
179+
} else {
180+
return String.valueOf(getText());
181+
}
174182
}
175183

176184
@Override
@@ -272,9 +280,6 @@ private void updateColorForLines(boolean next, boolean current) {
272280
}
273281
}
274282

275-
public void setOnCompleteListener(OnCompleteListener listener) {
276-
completeListener = listener;
277-
}
278283

279284
@Override
280285
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@@ -290,4 +295,14 @@ public void afterTextChanged(Editable s) {
290295
completeListener.onComplete(String.valueOf(s));
291296
}
292297
}
298+
299+
300+
@Override
301+
public void setOnClickListener(OnClickListener l) {
302+
mClickListener = l;
303+
}
304+
305+
public void setOnCompleteListener(OnCompleteListener listener) {
306+
completeListener = listener;
307+
}
293308
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<set xmlns:android="http://schemas.android.com/apk/res/android">
3+
<translate android:duration="50"
4+
android:fromXDelta="-3%"
5+
android:repeatCount="3"
6+
android:repeatMode="reverse"
7+
android:toXDelta="3%"/>
8+
</set>

readme.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ With stunning animation, and high customizability.
99

1010
![Demo with underline](images/demo2.gif)
1111
![Dark theme demo](images/dark_theme_demo.gif)
12+
![Error animation](images/err_anim.gif)
1213

1314
## Packed with features
1415
- Add custom character limit.
@@ -51,6 +52,18 @@ Step 3. Add OtpEditText to your layout file
5152
/>
5253
```
5354

55+
## Usage
56+
57+
### GetOtpValue() method usage
58+
```
59+
String otpValue = otpEditText.getOtpValue();
60+
if (otpValue != null) {
61+
textDisplay.setText("Entered Value: " + otpEditText.getOtpValue());
62+
}
63+
```
64+
This method when invoked, will either return the OTP value, in case this is invoked before the user inputs the desired length of characters, a shake animation will be triggered implicitly.
65+
66+
5467
## How to customize the view.
5568
### Setting desired length for the OTP(One time password code)
5669

0 commit comments

Comments
 (0)