Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created July 19, 2019 13:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/44a5419381a2f8ec4f1f48213bd83b8c to your computer and use it in GitHub Desktop.
Save ssaurel/44a5419381a2f8ec4f1f48213bd83b8c to your computer and use it in GitHub Desktop.
MainActivity for the Password Strength Calculator App for Android tutorial on the SSaurel's Channel
package com.ssaurel.passwordstrength;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private EditText editText;
private View root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = findViewById(R.id.root);
textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);
// now we set Text Watcher on the edit text
// to update the password strength in real time
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
calculatePasswordStrength(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void calculatePasswordStrength(String str) {
// Now, we need to define a PasswordStrength enum
// with a calculate static method returning the password strength
PasswordStrength passwordStrength = PasswordStrength.calculate(str);
textView.setText(passwordStrength.msg);
root.setBackgroundColor(passwordStrength.color);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment