I have two EditTexts for a username and password, each having the text “ENTER A USERNAME” and “ENTER A PASSWORD” respectively, as their default text.
Their text colors are a light gray. Now when they get focused they’re supposed to change text colors as well as the password edittext’s inputType to a password type (on top of other things but that works).
It successfully changes the text color to black at first but fails to change back and the inputType never changes.
Please suggest me where I am lacking, below is my code:
OnFocusChangeListener pwListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
//if it has focus then clear the text and make text black
if(((EditText) v).getText().toString().compareTo(getString(R.string.passwordField)) == 0) {
((EditText) v).setTextColor(R.color.Black);
((EditText) v).setText(R.string.blank);
((EditText) v).setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
} else if(!hasFocus) {
//if it loses focus and nothing was inputed then change back to default
if(isEmpty(((EditText) v).getText().toString())) {
((EditText) v).setTextColor(R.color.TextInput);
((EditText) v).setText(R.string.passwordField);
((EditText) v).setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
}
}
}
};
Thats the passwords edit text and this is its XML:
<EditText
android:id="@+id/passwordInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_marginTop="5dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:paddingTop="15dip"
android:paddingBottom="15dip"
android:text="@string/passwordField"
android:textColor="@color/TextInput"
android:textSize="25sp"
android:gravity="center_vertical"
android:ems="10" >
</EditText>
Why you dont set the ENTER A USERNAME” and “ENTER A PASSWORD” as hint text in Edittext using
It will automatically disappear when user clicked on Edittext.