Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically setting up password dots in EditText

Tags:

android

I've been attempting to programmatically set up 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 edittext 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 have been unsuccessful using all of the above approaches as the edittext still displays the characters when a user types into them.

I'm aware in XML how to set up an edittext to be a password field but I need to this programmatically.

like image 610
TokTok123 Avatar asked Nov 07 '13 08:11

TokTok123


2 Answers

You don't need to create another password transformation method, you could do that.

password.setTransformationMethod(PasswordTransformationMethod.getInstance());
like image 165
Heitor Colangelo Avatar answered Oct 07 '22 01:10

Heitor Colangelo


In onCreate() add these two lines while creating EditText

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

then create MyPasswordTransformationMethod class in same activity like 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
        }
    }
};
like image 34
krishna Avatar answered Oct 07 '22 03:10

krishna