Programmatically Setting Password Points in EditText

I tried to programmatically configure my edittext as a password field as follows:

Method 1:

password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); 

Method 2:

 password.setTransformationMethod(PasswordTransformationMethod.getInstance()); 

Method 3:

 password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 

Method 4:

  public class MyPasswordTransformationMethod extends PasswordTransformationMethod { @Override public CharSequence getTransformation(CharSequence source, View view) { return new PasswordCharSequence(source); } private class PasswordCharSequence implements CharSequence { private CharSequence mSource; public PasswordCharSequence(CharSequence source) { mSource = source; // Store char sequence } public char charAt(int index) { return '*'; // This is the important part } public int length() { return mSource.length(); // Return default } public CharSequence subSequence(int start, int end) { return mSource.subSequence(start, end); // Return default } } }; // Call the above class using this: text.setTransformationMethod(new MyPasswordTransformationMethod()); 

I created my edit text as follows:

  // Create the password edittext EditText etPwrd = new EditText(this); // Customise the password edittext etPwrd.setLayoutParams(etPwrdParams); etPwrd.setBackgroundResource(R.drawable.etlogin); etPwrd.setTextSize(18f); etPwrd.setLongClickable(false); etPwrd.setPadding (5,0,0,0); etPwrd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); etPwrd.setTransformationMethod(new MyPasswordTransformationMethod()); etPwrd.setTypeface(officialRegularFont); //etPwrd.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); //etPwrd.setTransformationMethod(PasswordTransformationMethod.getInstance()); etPwrd.setSingleLine(); etPwrd.setHint(R.string.password_text); etPwrd.setCustomSelectionActionModeCallback(new ActionMode.Callback() { @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { return false; } @Override public void onDestroyActionMode(ActionMode mode) { } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } }); 

I was not able to use all of the above approaches, as the edit text still displays characters when the user enters them.

In XML, I know how to set the edit text as a password field, but I need it programmatically.

+6
source share
2 answers

You do not need to create another password conversion method, you can do it.

 password.setTransformationMethod(PasswordTransformationMethod.getInstance()); 
+4
source

In onCreate (), add these two lines when creating the EditText

  et2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD) et2.setTransformationMethod(new MyPasswordTransformationMethod()); 

then create the MyPasswordTransformationMethod class in the same action as this

 public class MyPasswordTransformationMethod extends PasswordTransformationMethod { @Override public CharSequence getTransformation(CharSequence source, View view) { return new PasswordCharSequence(source); } private class PasswordCharSequence implements CharSequence { private CharSequence mSource; public PasswordCharSequence(CharSequence source) { mSource = source; // Store char sequence } public char charAt(int index) { return '*'; // This is the important part } public int length() { return mSource.length(); // Return default } public CharSequence subSequence(int start, int end) { return mSource.subSequence(start, end); // Return default } } }; 
+4
source

Source: https://habr.com/ru/post/957566/


All Articles