This is a different method from Password Visibility Toggle. Sampler is here
Pattern0 is a guy, but this time I will introduce Pattern1. I'm using TextInputLayout for clarity, but it's okay not to use it I want to customize without relying on Password Visibility Toggle! It is for people.
XML
activity_main.xml
<!--Abbreviation-->
<!-- Pattern 1 -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- password -->
<android.support.design.widget.TextInputEditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password"/>
</android.support.design.widget.TextInputLayout>
<!--Show / hide password-->
<CheckBox
android:id="@+id/password_visibility"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="Show password"/>
<!--Abbreviation-->
Activity
MainActivity.java
//Abbreviation
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextInputEditText password = (TextInputEditText)findViewById(R.id.password);
CheckBox passwordVisibilityCheckBox = (CheckBox) findViewById(R.id.password_visibility);
//Password switching checkbox
passwordVisibilityCheckBox.setOnClickListener((View v) -> {
//Save password cursor position
int cursorPos = password.getSelectionStart();
boolean checked = passwordVisibilityCheckBox.isChecked();
if (checked) {
//Password visualization
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
//Cursor position set
password.setSelection(cursorPos);
} else {
//Password invisibility
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
password.setSelection(cursorPos);
}
});
}
}
Yes completed!
There is a better implementation! I'm waiting for your comment! !!
Recommended Posts