# Android Password Strength Meter
Password strength meter is an easy-to-implement and flexible password strength indicator for Android. It is fully customizable and features an animated strength indicator and a matching label.
<img src="https://media2.giphy.com/media/PMuSdJrT7U1G0kdaCr/giphy.gif" width="450">
## Usage
**Project level build.gradle**
~~~~gradle
allprojects {
repositories {
mavenCentral()
}
}
~~~~
App level build.gradle
~~~~gradle
dependencies {
implementation 'nu.aaro.gustav:passwordstrengthmeter:0.4'
}
~~~~
## Examples
### XML
PasswordStrengthMeter can be initialized by defining it in a layout XML file, for example:
~~~~xml
<nu.aaro.gustav.passwordstrengthmeter.PasswordStrengthMeter
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/passwordInputMeter"
app:strengthBarHeight="5dp"
app:animateChanges="true"
app:showStrengthLabel="true"
app:showStrengthBar="true"
app:animationDuration="300"
app:labelTextSize="12sp"/>
~~~~
Then all you have to do is to set it up like this in your activity of fragment
~~~~java
PasswordStrengthMeter meter = findViewById(R.id.passwordInputMeter);
meter.setEditText(passwordInputEditText);
~~~~
#### XML properties
* **showStrengthBar:** Define whether or not the view should show the strength indicator bar. Default: true
* **strengthBarHeight:** The height of the strength indicator bar. Default: 5dp
* **animateChanges:** Define whether or not the bar should animate changes. Default: true
* **animationDuration:** The duration of the password strength level change animation in ms. Default: 300ms.
* **showStrengthLabel:** Define whether or not the view should show a label which show the current strength level display name. Default: true
* **labelTextSize:** The label text size. Default: 12sp
### Java
PasswordStrengthMeter can also be initalized programatically:
~~~~java
PasswordStrengthMeter meter = new PasswordStrengthMeter(this);
meter.setEditText(passwordInput);
linearLayout.addView(meter);
// And some customization can be done by:
meter.setAnimationDuration(300);
meter.setShowStrengthIndicator(true);
meter.setShowStrengthLabel(true);
~~~~
## Customization
Apart from the basic cosmetic customization described above, PasswordStrengthMeter can also be customized in other aspects.
### Password strength calculation algorithm
PasswordStrengthMeter implements a default algorithm for password strength estimation, although it is very basic and should not be considered the main contribution of this library. Instead, I recommend you to implement an algorithm that fits your system the best using the `PasswordStrengthCalculator`interface:
~~~~java
meter.setPasswordStrengthCalculator(new PasswordStrengthCalculator() {
@Override
public int calculatePasswordSecurityLevel(String password) {
// Do some calculation and return an int corresponding to the "points" or "level" the user password got
return points;
}
@Override
public int getMinimumLength() {
// Define the minimum length of a password. Anything below this should always yield a score of 0
return 8;
}
@Override
public boolean passwordAccepted(int level) {
// Define whether or not the level is an accepted level or not.
return level > 3;
}
@Override
public void onPasswordAccepted(String password) {
// Called when the password entered meets your requirements of length and strength levels
}
});
~~~~
### Password strength levels
PasswordStrengthMeter has 5 (or 6 if you count level 0) default password strength levels. These are simply `PasswordStrengthLevel` objects that defines a display name and a color associated with the level. These are stored in an array, where the index corresponds to the numerical "level" or "score" from the password strength calculation algorithm.
The default levels are defined as follows:
~~~~java
PasswordStrengthLevel[] strengthLevels = {
new PasswordStrengthLevel("Too short", android.R.color.darker_gray), // level 0
new PasswordStrengthLevel("Weak", android.R.color.holo_red_dark), // level 1
new PasswordStrengthLevel("Fair", android.R.color.holo_orange_dark), // level 2
new PasswordStrengthLevel("Good", android.R.color.holo_orange_light), // level 3
new PasswordStrengthLevel("Strong", android.R.color.holo_blue_light), // level 4
new PasswordStrengthLevel("Very strong", android.R.color.holo_green_dark)}; // level 5
~~~~
These levels can easily be changed by simply creating a similar array and call `setStrengthLevels`:
~~~~java
PasswordStrengthMeter meter = new PasswordStrengthMeter(this);
meter.setStrengthLevels(new PasswordStrengthLevel[]{
new PasswordStrengthLevel("Level 0", android.R.color.white),
new PasswordStrengthLevel("Level 1", android.R.color.holo_red_light),
new PasswordStrengthLevel("Level 2", android.R.color.holo_orange_light),
new PasswordStrengthLevel("Level 3", android.R.color.holo_green_light)});
~~~~
**NOTE** that for the best result, you should provide the meter with a number of levels that is adapted for the strength estimation algorithm used. I.e. if the algorithm yields a level between 0 and 5 (as the default implementation), you should provide the meter with a total of 6 strength levels.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
适用于Android的易于实施且完全可定制的密码强度.zip (43个子文件)
AndroidPasswordStrengthMeter-master
gradle.properties 730B
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 230B
PassWordStrengthMeter
src
main
java
nu
aaro
gustav
passwordstrengthmeter
StrengthIndicatorView.java 4KB
PasswordStrengthCalculator.java 954B
PasswordStrengthLevel.java 514B
PasswordStrengthMeter.java 13KB
res
values
strings.xml 84B
attrs.xml 485B
AndroidManifest.xml 123B
proguard-rules.pro 751B
build.gradle 1KB
.gitignore 7B
sample
src
main
java
nu
aaro
gustav
sample
MainActivity.java 2KB
res
mipmap-xxhdpi
ic_launcher_round.png 10KB
ic_launcher.png 6KB
mipmap-hdpi
ic_launcher_round.png 5KB
ic_launcher.png 3KB
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-anydpi-v26
ic_launcher.xml 272B
ic_launcher_round.xml 272B
mipmap-mdpi
ic_launcher_round.png 3KB
ic_launcher.png 2KB
mipmap-xxxhdpi
ic_launcher_round.png 15KB
ic_launcher.png 9KB
mipmap-xhdpi
ic_launcher_round.png 7KB
ic_launcher.png 4KB
values
colors.xml 208B
strings.xml 69B
styles.xml 383B
layout
activity_main.xml 1KB
drawable
ic_launcher_background.xml 5KB
AndroidManifest.xml 715B
proguard-rules.pro 751B
build.gradle 985B
.gitignore 7B
LICENSE 11KB
gradlew.bat 2KB
build.gradle 449B
settings.gradle 44B
gradlew 5KB
.gitignore 118B
README.md 5KB
共 43 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9149
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功