-4

I need to change hint color property dynamically when its unfocused. Is it possible to change the hint color dynamically(Not in focused state).

Actual Design

enter image description here

and my xml

enter image description here

I can't change the hint color property programmatically when it is unfocused. I am stuck. Is it possible to do? Please help me. Here is my code -

         btnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String email = emailEditText.getText().toString();
                final String pass = passEditText.getText().toString();
                if (email.isEmpty()) {
                    inputLayoutEmail.setHint("email is required to login");
                    inputLayoutEmail.setHintTextAppearance(R.style.EditTextLayout);
                    inputLayoutEmail.setError(" ");
                    emailEditText.setHintTextColor(getResources().getColor(R.color.primary_dark));

                }
                if (pass.isEmpty()) {
                    inputLayoutPassword.setHint("password is required to login");
                   passEditText.setHintTextColor(getResources().getColor(R.color.primary_dark));

                    inputLayoutPassword.setHintTextAppearance(R.style.EditTextLayout);
                    inputLayoutPassword.setError(" ");
                    inputLayoutPassword.clearFocus();
                    passEditText.setHint("Password is required to login");
                  passEditText.setHintTextColor(Color.RED);
                }
Mikhail_Sam
  • 10,602
  • 11
  • 66
  • 102

3 Answers3

0

Use this code this may help you.

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (editText1.getText().toString().length() == 0) {
                    editText1.setHintTextColor(Color.RED);
                }
                if (editText2.getText().toString().length() == 0) {
                    editText2.setHintTextColor(Color.RED);
                }
            }
        });
Mayank Pandya
  • 265
  • 3
  • 14
0
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (!b) {
         editText.setHintTextColor(getResources().getColor(R.color.red));
            } else {
             // set which color you want when edittext is focused
        }
    });

OR you can use theme also to set color

<style name="InputLayoutTheme" parent="Widget.Design.TextInputLayout">
     // Whenever input layout is un-focused, below is the color for label 
    <item name="android:textColorHint">@color/dark_gray</item>
    // Whenever input layout is un-focused, below is the underline color 
    <item name="colorControlNormal">@color/dark_gray</item>
    <!-- Whenever input layout is focused, below is the color for label,    cursor & underline-->
    <item name="colorControlActivated">@color/button_text_blue2</item>
</style>
  • ...when i click login without filling any fields,in that case both field's hint color will change.here m not giving any focus.. – Vivek Annamlai Jul 28 '17 at 05:15
  • add this style to style file – Priyanka Madgundi Jul 28 '17 at 05:28
  • set following parameter to textInput layout android:theme="@style/InputLayoutTheme" app:errorEnabled="true" app:errorTextAppearance="@style/ErrorTextAppearance" – Priyanka Madgundi Jul 28 '17 at 05:29
0

Please try this code. It Changes Hint text color as RED when you click on Login and if the feilds are empty. And it rechanges hint text color as BLACK when you click to write anything in edittext. Hope that helps..

Here is my Activity -

public class Main2Activity extends AppCompatActivity implements View.OnFocusChangeListener{

    EditText emailEditText, passEditText;
    Button btnSignUp;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.amin);

        emailEditText = (EditText) findViewById(R.id.edt1);
        passEditText = (EditText) findViewById(R.id.edt2);
        btnSignUp = (Button) findViewById(R.id.btnSignUp);

        emailEditText.setOnFocusChangeListener(this);
        passEditText.setOnFocusChangeListener(this);
        btnSignUp.setOnFocusChangeListener(this);

        btnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                final String email = emailEditText.getText().toString();
                final String pass = passEditText.getText().toString();
                if (email.isEmpty()) {

                    emailEditText.setHintTextColor(Color.RED);

                }
                if (pass.isEmpty()) {

                    passEditText.setHintTextColor(Color.RED);

                    passEditText.setHint("Password is required to login");
                    passEditText.setHintTextColor(Color.RED);
                }
            }
        });
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
            if(v instanceof EditText)
            {
                ((EditText)v).setHintTextColor(Color.BLACK);
                v.requestLayout();
                v.refreshDrawableState();

            }
    }
}

And My XML -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/edt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="PLease enter Id"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textColor="@color/black"/>

    <EditText
        android:id="@+id/edt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="PLease enter Password"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textColor="@color/black" />


    <Button
        android:id="@+id/btnSignUp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        />
</LinearLayout>
Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34