Skip to content

Instantly share code, notes, and snippets.

@abualgait
Last active September 1, 2021 13:40
Show Gist options
  • Select an option

  • Save abualgait/4ab835e07d2382aa0f19e962870b86c8 to your computer and use it in GitHub Desktop.

Select an option

Save abualgait/4ab835e07d2382aa0f19e962870b86c8 to your computer and use it in GitHub Desktop.
Password Length Strength (Custom View)
/**
* at your activity/fragment just add onTextChanged Listener and use the fun. setPassword
*/
edPassword.onTextChanged {
passwordStrenghtView.setPassword(it)
}
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/sixCharachters"
style="@style/LinearProgressIndicator"
android:layout_width="0dp"
android:layout_weight="1" />
<Space
android:layout_width="@dimen/_10sdp"
android:layout_height="0dp" />
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/oneUpperCase"
style="@style/LinearProgressIndicator"
android:layout_width="0dp"
android:layout_weight="1" />
<Space
android:layout_width="@dimen/_10sdp"
android:layout_height="0dp" />
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/oneSymbol"
style="@style/LinearProgressIndicator"
android:layout_width="0dp"
android:layout_weight="1" />
<Space
android:layout_width="@dimen/_10sdp"
android:layout_height="0dp" />
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/twoDigits"
style="@style/LinearProgressIndicator"
android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
<Space
android:layout_width="0dp"
android:layout_height="@dimen/_10sdp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<View
android:id="@+id/sixCharachtersBullet"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/circle_bullet_unactivated" />
<Space
android:layout_width="@dimen/_10sdp"
android:layout_height="0dp" />
<TextView
style="@style/TextAppearance.Text.LightGotham"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6 lettres"
android:textColor="@color/greyish_brown" />
</LinearLayout>
<Space
android:layout_width="0dp"
android:layout_height="@dimen/_10sdp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<View
android:id="@+id/oneSymbolBullet"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/circle_bullet_unactivated" />
<Space
android:layout_width="@dimen/_10sdp"
android:layout_height="0dp" />
<TextView
style="@style/TextAppearance.Text.LightGotham"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 symbole"
android:textColor="@color/greyish_brown" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<View
android:id="@+id/oneUpperCaseBullet"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/circle_bullet_unactivated" />
<Space
android:layout_width="@dimen/_10sdp"
android:layout_height="0dp" />
<TextView
style="@style/TextAppearance.Text.LightGotham"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 majuscule"
android:textColor="@color/greyish_brown" />
</LinearLayout>
<Space
android:layout_width="0dp"
android:layout_height="@dimen/_10sdp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<View
android:id="@+id/twoDigitsBullet"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/circle_bullet_unactivated" />
<Space
android:layout_width="@dimen/_10sdp"
android:layout_height="0dp" />
<TextView
style="@style/TextAppearance.Text.LightGotham"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2 chiffres"
android:textColor="@color/greyish_brown" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</layout>
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.hpa.app.R
import com.hpa.app.databinding.PasswordStrenghtViewBinding
import com.hpa.app.shared.util.isPasswordLengthLessThanSix
import java.util.regex.Pattern
class PasswordStrenghtView<out T>(context: Context, attrs: AttributeSet) :
LinearLayout(context, attrs) {
var binding: PasswordStrenghtViewBinding =
DataBindingUtil.inflate(
LayoutInflater.from(context),
R.layout.password_strenght_view,
this,
true
)
var passwordStrenghtPassed: Boolean = false
init {
isClickable = true
isFocusable = true
val attributes = context.obtainStyledAttributes(attrs, R.styleable.PasswordStrenghtView)
attributes.recycle()
}
enum class PasswordStrength(var enabled: Boolean) {
SixCharachters(false),
OneUpperCase(false),
OneSymbol(false),
TwoDigits(false)
}
fun activateBullet(view: View, activate: Boolean) {
if (activate) {
view.background =
context.let { ContextCompat.getDrawable(it, R.drawable.circle_bullet) }
} else {
view.background =
context.let { ContextCompat.getDrawable(it, R.drawable.circle_bullet_unactivated) }
}
}
fun setPasswordStrength(type: PasswordStrength) {
when (type) {
PasswordStrength.SixCharachters -> {
passwordStrenghtPassed = PasswordStrength.SixCharachters.enabled
if (PasswordStrength.SixCharachters.enabled) {
binding.sixCharachters.progress = 100
activateBullet(binding.sixCharachtersBullet, true)
} else {
binding.sixCharachters.progress = 0
activateBullet(binding.sixCharachtersBullet, false)
}
}
PasswordStrength.OneUpperCase -> {
passwordStrenghtPassed = PasswordStrength.OneUpperCase.enabled
if (PasswordStrength.OneUpperCase.enabled) {
binding.oneUpperCase.progress = 100
activateBullet(binding.oneUpperCaseBullet, true)
} else {
binding.oneUpperCase.progress = 0
activateBullet(binding.oneUpperCaseBullet, false)
}
}
PasswordStrength.OneSymbol -> {
passwordStrenghtPassed = PasswordStrength.OneSymbol.enabled
if (PasswordStrength.OneSymbol.enabled) {
binding.oneSymbol.progress = 100
activateBullet(binding.oneSymbolBullet, true)
} else {
binding.oneSymbol.progress = 0
activateBullet(binding.oneSymbolBullet, false)
}
}
PasswordStrength.TwoDigits -> {
passwordStrenghtPassed = PasswordStrength.TwoDigits.enabled
if (PasswordStrength.TwoDigits.enabled) {
binding.twoDigits.progress = 100
activateBullet(binding.twoDigitsBullet, true)
} else {
binding.twoDigits.progress = 0
activateBullet(binding.twoDigitsBullet, false)
}
}
}
}
fun setPassword(password: String) {
setPasswordStrength(PasswordStrength.SixCharachters.also {
it.enabled = isPasswordLengthLessThanSix(password).not()
})
setPasswordStrength(PasswordStrength.OneUpperCase.also {
it.enabled = Pattern.compile(".*[A-Z].*").matcher(password).matches()
})
setPasswordStrength(PasswordStrength.OneSymbol.also {
it.enabled = Pattern.compile("[^a-zA-Z0-9]").matcher(password).find()
})
setPasswordStrength(PasswordStrength.TwoDigits.also {
it.enabled = Pattern.compile("(.*?\\d){2,}.*").matcher(password).matches()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment