Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1K views

Android - Programatically Setting Up Password Dots in EditText - Stack Overflow

The document discusses programmatically setting up an EditText field to display password characters as dots instead of the actual characters. Method 1 and 2 using the InputType and TransformationMethod alone did not work for the user. Method 4 involving creating a custom PasswordTransformationMethod class did work when the transformation method was added at the end of the EditText initialization as suggested by another user. The key steps are to set the input type to password and assign the custom transformation method.

Uploaded by

Vikesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Android - Programatically Setting Up Password Dots in EditText - Stack Overflow

The document discusses programmatically setting up an EditText field to display password characters as dots instead of the actual characters. Method 1 and 2 using the InputType and TransformationMethod alone did not work for the user. Method 4 involving creating a custom PasswordTransformationMethod class did work when the transformation method was added at the end of the EditText initialization as suggested by another user. The key steps are to set the input type to password and assign the custom transformation method.

Uploaded by

Vikesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

3/30/2016

androidProgramaticallysettinguppassworddotsinEditTextStackOverflow

29

help

ProgramaticallysettinguppassworddotsinEditText
I'vebeenattemptingtoprogrmaticallysetupmyedittextasapasswordfieldasfollows:
Method1:
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

Method2:
password.setTransformationMethod(PasswordTransformationMethod.getInstance());

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

Method4:
publicclassMyPasswordTransformationMethodextendsPasswordTransformationMethod{
@Override
publicCharSequencegetTransformation(CharSequencesource,Viewview){
returnnewPasswordCharSequence(source);
}
privateclassPasswordCharSequenceimplementsCharSequence{
privateCharSequencemSource;
publicPasswordCharSequence(CharSequencesource){
mSource=source;//Storecharsequence
}
publiccharcharAt(intindex){
return'*';//Thisistheimportantpart
}
publicintlength(){
returnmSource.length();//Returndefault
}
publicCharSequencesubSequence(intstart,intend){
returnmSource.subSequence(start,end);//Returndefault
}
}
};
//Calltheaboveclassusingthis:
text.setTransformationMethod(newMyPasswordTransformationMethod());

Icreatedmyedittextasfolllows:
//Createthepasswordedittext
EditTextetPwrd=newEditText(this);
//Customisethepasswordedittext
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(newMyPasswordTransformationMethod());
etPwrd.setTypeface(officialRegularFont);
//etPwrd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//etPwrd.setTransformationMethod(PasswordTransformationMethod.getInstance());
etPwrd.setSingleLine();
etPwrd.setHint(R.string.password_text);
etPwrd.setCustomSelectionActionModeCallback(newActionMode.Callback(){
@Override
publicbooleanonActionItemClicked(ActionModemode,MenuItemitem){
returnfalse;
}
@Override
publicbooleanonCreateActionMode(ActionModemode,Menumenu){
returnfalse;
}
@Override
publicvoidonDestroyActionMode(ActionModemode){
}
@Override
publicbooleanonPrepareActionMode(ActionModemode,Menumenu){
returnfalse;
}
});

Ihavebeenunsuccessfulusingalloftheaboveapproachesastheedittextstilldisplaysthecharacterswhenauser
typesintothem.
I'mawareinXMLhowtosetupanedittexttobeapasswordfieldbutIneedtothisprogrammatically.
Thanksforanysuggestions.

http://stackoverflow.com/questions/19831574/programaticallysettinguppassworddotsinedittext

1/3

3/30/2016

androidProgramaticallysettinguppassworddotsinEditTextStackOverflow

android
editedNov7'13at9:40

askedNov7'13at8:58

TokTok123
317

Method4shouldwork.mightbeyouwouldhavemissedaddinginputtypeforEditTextinXMLfile.krishna
Nov7'13at9:12

addthisline'etPwrd.setTransformationMethod(newMyPasswordTransformationMethod())'after
'etPwrd.setCustomSelectionActionModeCallback'anditshouldwork krishnaNov7'13at9:57

@krishna:THANKS..ITWORKED!!!reallyappreciateyourhelp!!! TokTok123 Nov7'13at10:04

Welcome,AlwaysDontforgottoaddsetTransformationMethod()atendofalldeclarationofedittext
krishnaNov7'13at10:06

Yesindeed..attheveryend..cheers TokTok123 Nov7'13at10:07

19

2Answers

Youdon'tneedtocreateanotherpasswordtransformationmethod,youcoulddothat.
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
answeredJan29'15at13:21

HeitorColangelo
79

Note:Thisshowsthecharacterwhiletypingandmakesitdotafterfewmilliseconds.krishnaFeb20'15
at6:39

+1forhereisbestanswernAkhmedov Mar17'15at14:50

InonCreate()addthesetwolineswhilecreatingEditText
et2.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD)
et2.setTransformationMethod(newMyPasswordTransformationMethod());

thencreate MyPasswordTransformationMethod classinsameactivitylikethis


publicclassMyPasswordTransformationMethodextendsPasswordTransformationMethod{
@Override
publicCharSequencegetTransformation(CharSequencesource,Viewview){
returnnewPasswordCharSequence(source);
}
privateclassPasswordCharSequenceimplementsCharSequence{
privateCharSequencemSource;
publicPasswordCharSequence(CharSequencesource){
mSource=source;//Storecharsequence
}
publiccharcharAt(intindex){
return'*';//Thisistheimportantpart
}
publicintlength(){
returnmSource.length();//Returndefault
}
publicCharSequencesubSequence(intstart,intend){
returnmSource.subSequence(start,end);//Returndefault
}
}
};
editedNov7'13at9:17

answeredNov7'13at9:00

krishna
2,996

18

41

thanks.i'llgiveitago. TokTok123 Nov7'13at9:01

HIkrishna,theprogrammaticbitdoesn'twork.AsfortheXMLsinceI'mdevelopingeverything
progrmmaticallyican'tcallanedittextfromanXMLfile TokTok123 Nov7'13at9:14

thenwhilecreatingEditTextaddthisline
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD)krishnaNov7'13at9:15

checkmyedittedanswerkrishnaNov7'13at9:17

Ifyoustillhaveproblempostthecodewereyouwascreatingedittextprogramatically krishnaNov7'13
at9:24

http://stackoverflow.com/questions/19831574/programaticallysettinguppassworddotsinedittext

2/3

3/30/2016

androidProgramaticallysettinguppassworddotsinEditTextStackOverflow

http://stackoverflow.com/questions/19831574/programaticallysettinguppassworddotsinedittext

3/3

You might also like