1- Add import PSMeter to your project.
implementation "com.github.baianat:PSMeter-Android:${VERSION}"
2- Add PSMeterView to your XML
<com.baianat.psmeter.pass_meter_view.PsMeterView
android:id="@+id/psMeterView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwordEditText"
/>
3- attach your password editText with PSMeterView
[EditText].addTextChangedListener {
attachPsStrengthEstimator(it.toString())
}
Note:
attachPsStrengthEstimator
should be called whenever the password text is changed to update the indicator appopriately.
You can customize PSMeter for these attributes:
Attribute | Definition | Default value |
---|---|---|
psBackgroundProgressColor | The background color of the strength bar | android.R.color.white |
psLabel | The title across the strength value | "Strength" |
psTextColor | The text color for the title | android.R.color.black |
psFont | The font for the strength labels | system font with 12sp size |
<com.baianat.psmeter.PsMeterView
android:id="@+id/psMeterView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwordEditText"
app:psBackgroundProgressColor="@android:color/holo_green_light"
app:psLabel="empty"
app:psFont="@font/app_font_bold"
app:psTextColor="@android:color/holo_green_dark"
/>
You can also customize text, text color, progress color for each of the 6 strength states.
These attributes are defined using PsStateDecorator class as follows...
PsStateDecorator(
psLabel = R.string.very_week,
pasTextColor = R.color.psColorVeryWeek,
psProgressColor = R.color.psColorVeryWeek
)
To customize all the stated you only have to set the six decorators for the six different states (empty, very weak, weak, fair, strong, very strong).
This can easily be achieved using PsMeterView.builder()
, the object combining all the decorators.
val psMeterBuilder = PsMeterView.builder()
.setEmptyStateDecorator(
PsStateDecorator(
psLabel = R.string.empty,
psTextColor = android.R.color.black,
psProgressColor = android.R.color.black,
)
).setVeryWeekStateDecorator(
PsStateDecorator(
psLabel = R.string.very_week,
pasTextColor = R.color.psColorVeryWeek,
psProgressColor = R.color.psColorVeryWeek
)
).setWeakStateDecorator(
PsStateDecorator(
psLabel = R.string.weak,
pasTextColor = R.color.psColorWeek,
psProgressColor = R.color.psColorWeek
)
).setFairStateDecorator(
PsStateDecorator(
psLabel = R.string.fair,
pasTextColor = R.color.psColorFair,
psProgressColor = R.color.psColorFair
)
).setStrongStateDecorator(
PsStateDecorator(
psLabel = R.string.strong,
pasTextColor = R.color.psColorStrong,
psProgressColor = R.color.psColorStrong
)
).setVeryStrongStateDecorator(
PsStateDecorator(
psLabel = R.string.very_strong,
pasTextColor = R.color.psColorVeryStrong,
psProgressColor = R.color.psColorVeryStrong
)
).build()
mPsMeterView.configurePsMeter(psMeterBuilder)
Note: don't forget to attach PSMeterStrengthEstimator with passwordEditText after that using
[EditText].addTextChangedListener { attachPsStrengthEstimator(it.toString()) }
You can observe for whenever the password strength changes, just use the following.
[PsMeterView].onPsMeterScoreChanged { passStrengthEnum, score, strengthMessage ->
Toast.makeText(this, "passStrengthEnum: $passStrengthEnum", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "score: $score", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "passStrengthMessage: $strengthMessage", Toast.LENGTH_SHORT).show()
}
Alternatively
1- you can get the current password strength phase by:
[PsMeterView].passStrengthEnum
2- you can get the current password strength score by:
[PsMeterView].passStrengthEnum.score
3- you can get the current password strength message by:
[PsMeterView].passStrengthMessage
Note: passStrengthEnum contains 6 values as follows...
enum class PassStrengthEnum(val score: Int) {
EMPTY(0),
VERY_WEAK(1),
Weak(2),
FAIR(3),
STRONG(4),
VERY_STRONG(5),
}
The default algorithm for determining password strength is based on GoSimpleLLC/nbvcxz
PSMeter uses GoSimpleLLC/nbvcxz, rxkotlin, rxAndroid under its hood, they gets automatically installed with library.