22
33import android .annotation .TargetApi ;
44import android .content .Context ;
5- import android .content .res .Resources ;
65import android .content .res .TypedArray ;
76import android .graphics .drawable .Drawable ;
87import android .os .Parcel ;
98import android .os .Parcelable ;
9+ import android .support .v4 .content .ContextCompat ;
1010import android .text .InputType ;
1111import android .util .AttributeSet ;
1212import android .view .MotionEvent ;
@@ -30,30 +30,35 @@ public PasswordEditText(Context context) {
3030
3131 public PasswordEditText (Context context , AttributeSet attrs ) {
3232 super (context , attrs );
33- init (context , attrs );
33+ init (context , attrs , 0 , 0 );
3434 }
3535
3636
3737 public PasswordEditText (Context context , AttributeSet attrs , int defStyleAttr ) {
3838 super (context , attrs , defStyleAttr );
39- init (context , attrs );
39+ init (context , attrs , 0 , 0 );
4040 }
4141
4242 @ TargetApi (21 )
4343 public PasswordEditText (Context context , AttributeSet attrs , int defStyleAttr , int defStyleRes ) {
4444 super (context , attrs , defStyleAttr , defStyleRes );
45- init (context , attrs );
45+ init (context , attrs , defStyleAttr , defStyleRes );
4646 }
4747
48- private void init (Context context , AttributeSet attrs ) {
49-
50- Resources res = context .getResources ();
51- TypedArray a = context .obtainStyledAttributes (attrs , R .styleable .password_edit_text );
52-
53- mHideDrawable = a .getDrawable (R .styleable .password_edit_text_hide_drawable );
54- mShowDrawable = a .getDrawable (R .styleable .password_edit_text_show_drawable );
55- mPasswordVisible = a .getBoolean (R .styleable .password_edit_text_password_visible , false );
56- a .recycle ();
48+ private void init (Context context , AttributeSet attrs , int defStyleAttr , int defStyleRes ) {
49+ TypedArray a = context .obtainStyledAttributes (attrs , R .styleable .password_edit_text , defStyleAttr , defStyleRes );
50+
51+ int hideDrawableResId =(R .drawable .in_anshul_hide_password );
52+ int showDrawableResId = R .drawable .in_anshul_show_password ;
53+ try {
54+ mPasswordVisible = a .getBoolean (R .styleable .password_edit_text_password_visible , false );
55+ hideDrawableResId = a .getResourceId (R .styleable .password_edit_text_hide_drawable , hideDrawableResId );
56+ showDrawableResId = a .getResourceId (R .styleable .password_edit_text_show_drawable , showDrawableResId );
57+ } finally {
58+ a .recycle ();
59+ mHideDrawable = ContextCompat .getDrawable (getContext (), hideDrawableResId );
60+ mShowDrawable = ContextCompat .getDrawable (getContext (), showDrawableResId );
61+ }
5762 mHideDrawable .setBounds (0 , 0 , mHideDrawable .getIntrinsicWidth (), mHideDrawable .getIntrinsicHeight ());
5863 mShowDrawable .setBounds (0 , 0 , mShowDrawable .getIntrinsicWidth (), mShowDrawable .getIntrinsicHeight ());
5964 if (mPasswordVisible ) {
@@ -73,14 +78,14 @@ private void togglePasswordView() {
7378 }
7479
7580 private void showPassword () {
76- setCompoundDrawables (null , null , mShowDrawable , null );
81+ setCompoundDrawables (null , null , mHideDrawable , null );
7782 setInputType (InputType .TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );
7883 setSelection (getText ().length ());
7984 mPasswordVisible = true ;
8085 }
8186
8287 private void hidePassword () {
83- setCompoundDrawables (null , null , mHideDrawable , null );
88+ setCompoundDrawables (null , null , mShowDrawable , null );
8489 setInputType (InputType .TYPE_CLASS_TEXT | InputType .TYPE_TEXT_VARIATION_PASSWORD );
8590 setSelection (getText ().length ());
8691 mPasswordVisible = false ;
0 commit comments