Skip to content

Commit

Permalink
Fixing Issues (underscore cursor and out of bounds index)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinSchildhorn committed Aug 7, 2022
1 parent f5a857e commit fe98d7f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ implementation 'com.github.KevinSchildhorn:OTPView:0.2.2'
| otp_itemWidth | dimension | width of each item | 44dp |
| otp_itemHeight | dimension | height of each item | 44dp |
| otp_cursorColor | color | color of the cursor | Black |
| otp_underscoreCursor (Experimental) | boolean | if true will show '_' instead of '|' for the cursor | false |
| otp_customCursor | drawable | custom drawable for the cursor, overrides otp_underscoreCursor | null |
| otp_allcaps | boolean | all caps(if fails set input type) | false |
| otp_marginBetween | dimension | margin between each item horizontally | 8dp |
| otp_isPassword | boolean | sets previously entered items as hidden | false |
Expand Down Expand Up @@ -69,9 +71,12 @@ otp_view.setOnFinishListener {
}
```
* `fun setOnCharacterUpdatedListener(func: (Boolean) -> Unit)` - listener callback for when a character was updated. Returns whether or not it was filled.
* `fun setText(str:String)` - Fills in as much of the text into the items as possible, with one character for each item. Overflow characters are discarded

* `fun clearText(showKeyboard: Boolean)` - Clears all the text, also has the option to hide or show the keyboard on the first item
* `fun setText(str:String)` - Fills in as much of the text into the items as possible, with one character for each item. Overflow characters are discarded.
* `fun clearText(showKeyboard: Boolean)` - Clears all the text, also has the option to hide or show the keyboard on the first item.

* `fun copyText(showKeyboard: Boolean)` - Copies text from OTP View.
* `fun pasteText(showKeyboard: Boolean)` - Pastes text from OTP View, paste menu item should be overridden from Activity.

* `fun fitToWidth(width: Int)` - Fits the entire view to the width entered. Made so that you can dynamically resize the view.
* `fun isFilled(): Boolean` - Returns whether or not all the fields have been filled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class MainActivity : AppCompatActivity() {
fill_button.setOnClickListener {
otp_view.setText("ABCDEF_EXTRA")
}
paste_button.setOnClickListener {
otp_view.pasteText()
}

continue_button.setOnClickListener {
Toast.makeText(this, otp_view.getStringFromFields(), Toast.LENGTH_LONG).show()
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@
app:layout_constraintTop_toTopOf="parent"
/>

<EditText
android:id="@+id/text_to_copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/copy_text"
app:layout_constraintBottom_toTopOf="@+id/otp_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/fill_button"
/>

<Button
android:id="@+id/paste_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/paste_text"
app:layout_constraintTop_toBottomOf="@+id/otp_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/continue_button"
/>

<Button
android:id="@+id/continue_button"
android:layout_width="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<string name="app_name">OtpView Sample</string>
<string name="fill_view">Fill View</string>
<string name="continue_text">Continue</string>
<string name="paste_text">Paste</string>
<string name="copy_text">You can Copy TEST01 to test paste</string>
</resources>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ task clean(type: Delete) {
}

project.ext {
versionCode = 22
versionName = "0.2.2"
versionCode = 23
versionName = "0.2.3"
}
25 changes: 21 additions & 4 deletions otpView/src/main/java/com/kevinschildhorn/otpview/OTPView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

package com.kevinschildhorn.otpview

import android.content.ClipData
import android.content.ClipDescription.MIMETYPE_TEXT_PLAIN
import android.content.ClipboardManager
import android.content.Context
import android.content.res.Resources
import android.graphics.Color
Expand All @@ -35,13 +38,11 @@ import android.text.method.PasswordTransformationMethod
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.Gravity
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.*
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.LinearLayout
import androidx.core.content.ContextCompat.getSystemService
import androidx.core.widget.addTextChangedListener
import com.kevinschildhorn.otpview.otpview.R
import kotlinx.android.synthetic.main.otp_view_layout.view.*
Expand Down Expand Up @@ -474,6 +475,22 @@ class OTPView @JvmOverloads constructor(
it.isEnabled = enabled
}
}

fun copyText() {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip: ClipData = ClipData.newPlainText("Copied", getStringFromFields())
clipboard.setPrimaryClip(clip)
}

fun pasteText() {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
clipboard.primaryClip?.let {
val item = it.getItemAt(0)
val pasteData: String = item.text.toString()
setText(pasteData)
}
}

// endregion
}

Expand Down

0 comments on commit fe98d7f

Please sign in to comment.