PasswordEditText extends AppCompatEditText { @Override public void setInputType(int type) { Typeface typeface = getTypeface(); int selectionStart = getSelectionStart(); int selectionEnd = getSelectionEnd(); super.setInputType(type); setTypeface(typeface); setSelection(selectionStart, selectionEnd); } PasswordEditText(Context context);  PasswordEditText(Context context, AttributeSet attrs);  PasswordEditText(Context context, AttributeSet attrs, int defStyleAttr); @Override void setInputType(int type); @Override boolean onTouchEvent(MotionEvent event); boolean isPasswordVisible(); void setPasswordVisible(boolean visible); void togglePasswordVisibility(); @ColorInt int getTintColor(); void setTintColor(@ColorInt int tintColor); void setTintColorRes(@ColorRes int tintColorRes); void setShownDrawable(Drawable shownDrawable); void setShownDrawable(@DrawableRes int shownDrawableRes); void setHiddenDrawable(Drawable hiddenDrawable); void setHiddenDrawable(@DrawableRes int hiddenDrawableRes); void setToggleType(@ToggleType int toggleType); static final int TOGGLE_OPACITY; static final int TOGGLE_STRIKETHROUGH; }
@Test public void whenSetInputType_thenTypefaceUnchanged() { passwordView.setTypeface(Typeface.DEFAULT_BOLD); passwordView.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); assertThat(passwordView) .hasInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD) .hasTypeface(Typeface.DEFAULT_BOLD); }
@Test public void whenSetInputType_thenSelectionUnchanged() { int start = 10; int end = 12; String text = "a really long string with lots of content"; passwordView.setText(text); passwordView.setSelection(start, end); passwordView.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); assertThat(passwordView) .hasInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) .hasSelectionStart(start) .hasSelectionEnd(end); }