Ravindra
Ravindra

Reputation: 51

I can't create Password edittext programmatically?

setInputType() and setTransformationMethod() is not working while I creating Password EditText programmatically in android...

My Code :

EditText edtPassword = new EditText(getApplicationContext());
edtPassword.setLayoutParams(rowParams);
edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
edtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());

This is not working...

Upvotes: 5

Views: 1411

Answers (7)

Umer Bilal
Umer Bilal

Reputation: 69

password is not hiding by default that's why i applied

 Handler(Looper.getMainLooper()).postDelayed({
        this.edtv.transformationMethod = PasswordTransformationMethod.getInstance()
    },100)

this will work for me

Upvotes: 0

Ashokkumar
Ashokkumar

Reputation: 67

Add the following line in onCreate() method where you created the edittext

edtPassword.setTransformationMethod(new MyPasswordTransformationMethod());

After that create the PasswordTransformationMethod class in the same activity:

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; 
        }
        public char charAt(int index) {
            return '*'; 
        }
        public int length() {
            return mSource.length(); 
        }
        public CharSequence subSequence(int start, int end) {
            return mSource.subSequence(start, end); 
        }
    }
};

Upvotes: 0

ashfak
ashfak

Reputation: 304

you havent add the edittext in view ,so it may not working ... This code worked fine for me

LinearLayout mLinearLayout = new LinearLayout(this);
        mLinearLayout = (LinearLayout)findViewById(R.id.mylinearlayout);


            EditText lEditText = new EditText(this);
            lEditText .setLayoutParams(new           LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
                                                 LayoutParams.WRAP_CONTENT));
             lEditText.setText("Text Here");
             mLinearLayout.addView(lEditText);
             lEditText.setWidth(50);     // change width
             lEditText.setHeight(20); 

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

Upvotes: 2

Linga
Linga

Reputation: 10563

change

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

to

editPassword.setInputType(InputType.TYPE_CLASS_TEXT | inputType.TYPE_TEXT_VARIATION_PASSWORD);

For password style implementation, for the editText, the code is,

editPassword.setTransformationMethod
(new android.text.method.PasswordTransformationMethod().getInstance());

The setTransformationMethod function transforms the input value to a (dot) after some interval or after input of next character before the interval is completed.

Upvotes: 0

According to the TextView docs, the programmatic version of android:password is setTransformationMethod(). So something like:

mEdit.setTransformationMethod(PasswordTransformationMethod.getInstance()); should do the work.

If it doesn't work try this

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

Upvotes: 0

Shashank Agarwal
Shashank Agarwal

Reputation: 512

use

EditText edtPassword = new EditText(getApplicationContext());
edtPassword.setLayoutParams(rowParams);
edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
edtPassword.setTransformationMethod(new PasswordTransformationMethod());

Upvotes: 0

Hitman
Hitman

Reputation: 598

Try this code:

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

Without setting the transformation. Or if it does not work the setinput method, use the setTransformation but not the both at the same time.

Upvotes: 0

Related Questions