Last active
April 16, 2020 13:25
-
-
Save danherrera/2e56647ec9e5fda3c385f41cd53b8148 to your computer and use it in GitHub Desktop.
Android Pin/Code Entry Hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:padding="16dp"> | |
<EditText | |
android:id="@+id/textInput" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:hint="@string/enter_pin" | |
android:importantForAutofill="no" | |
android:inputType="number" | |
android:maxLength="4" | |
android:visibility="invisible" | |
android:textIsSelectable="false" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<TextView | |
android:id="@+id/digit1" | |
android:layout_width="72dp" | |
android:layout_height="80dp" | |
android:layout_marginTop="64dp" | |
android:gravity="center" | |
app:layout_constraintEnd_toStartOf="@+id/digit2" | |
app:layout_constraintHorizontal_chainStyle="spread" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<TextView | |
android:id="@+id/digit2" | |
android:layout_width="72dp" | |
android:layout_height="80dp" | |
android:gravity="center" | |
app:layout_constraintBaseline_toBaselineOf="@+id/digit1" | |
app:layout_constraintEnd_toStartOf="@+id/digit3" | |
app:layout_constraintHorizontal_chainStyle="spread" | |
app:layout_constraintStart_toEndOf="@+id/digit1" /> | |
<TextView | |
android:id="@+id/digit3" | |
android:layout_width="72dp" | |
android:layout_height="80dp" | |
android:gravity="center" | |
app:layout_constraintBaseline_toBaselineOf="@+id/digit1" | |
app:layout_constraintEnd_toStartOf="@+id/digit4" | |
app:layout_constraintHorizontal_chainStyle="spread" | |
app:layout_constraintStart_toEndOf="@+id/digit2" /> | |
<TextView | |
android:id="@+id/digit4" | |
android:layout_width="72dp" | |
android:layout_height="80dp" | |
android:gravity="center" | |
app:layout_constraintBaseline_toBaselineOf="@+id/digit1" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_chainStyle="spread" | |
app:layout_constraintStart_toEndOf="@+id/digit3" /> | |
<View | |
android:id="@+id/digit1Underline" | |
android:layout_width="56dp" | |
android:layout_height="8dp" | |
app:layout_constraintEnd_toEndOf="@+id/digit1" | |
app:layout_constraintStart_toStartOf="@+id/digit1" | |
app:layout_constraintTop_toBottomOf="@+id/digit1" /> | |
<View | |
android:id="@+id/digit2Underline" | |
android:layout_width="56dp" | |
android:layout_height="8dp" | |
app:layout_constraintEnd_toEndOf="@+id/digit2" | |
app:layout_constraintStart_toStartOf="@+id/digit2" | |
app:layout_constraintTop_toTopOf="@+id/digit1Underline" /> | |
<View | |
android:id="@+id/digit3Underline" | |
android:layout_width="56dp" | |
android:layout_height="8dp" | |
app:layout_constraintEnd_toEndOf="@+id/digit3" | |
app:layout_constraintStart_toStartOf="@+id/digit3" | |
app:layout_constraintTop_toTopOf="@+id/digit1Underline" /> | |
<View | |
android:id="@+id/digit4Underline" | |
android:layout_width="56dp" | |
android:layout_height="8dp" | |
app:layout_constraintEnd_toEndOf="@+id/digit4" | |
app:layout_constraintStart_toStartOf="@+id/digit4" | |
app:layout_constraintTop_toTopOf="@+id/digit1Underline" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Within onViewCreated | |
fun updateViewBasedOnPin(pin: String) { | |
binding.digit1.text = pin.getOrNull(0)?.toString() | |
binding.digit2.text = pin.getOrNull(1)?.toString() | |
binding.digit3.text = pin.getOrNull(2)?.toString() | |
binding.digit4.text = pin.getOrNull(3)?.toString() | |
binding.digit1Underline.setBackgroundColor(if (pin.isNotEmpty()) Color.YELLOW else Color.DKGRAY) | |
binding.digit2Underline.setBackgroundColor(if (pin.length > 1) Color.YELLOW else Color.DKGRAY) | |
binding.digit3Underline.setBackgroundColor(if (pin.length > 2) Color.YELLOW else Color.DKGRAY) | |
binding.digit4Underline.setBackgroundColor(if (pin.length > 3) Color.YELLOW else Color.DKGRAY) | |
} | |
binding.textInput.afterTextChanged { pin -> | |
updateViewBasedOnPin(pin) | |
} | |
updateViewBasedOnPin("") | |
listOf( | |
binding.digit1, | |
binding.digit2, | |
binding.digit3, | |
binding.digit4 | |
).forEach { | |
it.setOnClickListener { | |
if (binding.textInput.requestFocus()) { | |
val imm = getSystemService(requireContext(), InputMethodManager::class.java) | |
imm?.showSoftInput(view, InputMethodManager.SHOW_FORCED) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment