Krish
Krish

Reputation: 4242

How to change TextInputLayout color in android?

Hi I am new for android and in my app I am using TextInputLayout fields.

I set the EditText border color and I don't want to set edit-text background color but according to my below code, when I tap on the "SIGN UP" button edittext background color displays as red.

How can we change this background color?

edittext_background:-

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="@android:color/white" />

    <stroke
        android:width="1dip"
        android:color="@android:color/holo_purple" />

    <padding android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"/>

</shape>

styles:-

 <style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/splahbgcolor</item>
        <item name="android:textSize">14sp</item>
    </style>

main.xml:-

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="60dp">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout">

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/input_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:background="@drawable/edittext_background"
                android:ems="10"
                android:inputType="textEmailAddress"
                android:padding="8dp"
                android:textColor="@color/splahbgcolor"
                android:hint="@string/hint_name" />

        </android.support.design.widget.TextInputLayout>

MainActivity:-

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private EditText inputName, inputEmail, inputPassword;
    private TextInputLayout inputLayoutName, inputLayoutEmail, inputLayoutPassword;
    private Button btnSignUp;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        inputLayoutName = (TextInputLayout) findViewById(R.id.input_layout_name);
        inputLayoutEmail = (TextInputLayout) findViewById(R.id.input_layout_email);
        inputLayoutPassword = (TextInputLayout) findViewById(R.id.input_layout_password);

        inputName = (EditText) findViewById(R.id.input_name);
        inputEmail = (EditText) findViewById(R.id.input_email);
        inputPassword = (EditText) findViewById(R.id.input_password);

        btnSignUp = (Button) findViewById(R.id.btn_signup);

        inputName.addTextChangedListener(new MyTextWatcher(inputName));
        inputEmail.addTextChangedListener(new MyTextWatcher(inputEmail));
        inputPassword.addTextChangedListener(new MyTextWatcher(inputPassword));

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

                submitForm();
            }
        });
    }

    /**
     * Validating form
     */
    private void submitForm() {

        if (!validateName()) {
            return;
        }

        if (!validateEmail()) {
            return;
        }

        if (!validatePassword()) {
            return;
        }

        Toast.makeText(getApplicationContext(), "Thank You!", Toast.LENGTH_SHORT).show();
    }

    private boolean validateName() {

        if (inputName.getText().toString().trim().isEmpty()) {
            inputLayoutName.setError(getString(R.string.err_msg_name));
            requestFocus(inputName);
            return false;

        } else {
            inputLayoutName.setErrorEnabled(false);
        }

        return true;
    }

    private boolean validateEmail() {

        String email = inputEmail.getText().toString().trim();

        if (email.isEmpty() || !isValidEmail(email)) {
            inputLayoutEmail.setError(getString(R.string.err_msg_email));
            requestFocus(inputEmail);
            return false;
        } else {
            inputLayoutEmail.setErrorEnabled(false);
        }

        return true;
    }

    private boolean validatePassword() {

        if (inputPassword.getText().toString().trim().isEmpty()) {
            inputLayoutPassword.setError(getString(R.string.err_msg_password));
            requestFocus(inputPassword);
            return false;
        } else {
            inputLayoutPassword.setErrorEnabled(false);
        }

        return true;
    }

    private static boolean isValidEmail(String email) {

        return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }

    private void requestFocus(View view) {

        if (view.requestFocus()) {
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }

    private class MyTextWatcher implements TextWatcher {

        private View view;

        private MyTextWatcher(View view) {
            this.view = view;
        }

        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        public void afterTextChanged(Editable editable) {

            switch (view.getId()) {

                case R.id.input_name:
                    validateName();
                    break;

                case R.id.input_email:
                    validateEmail();
                    break;

                case R.id.input_password:
                    validatePassword();
                    break;
            }
        }
    }
}

enter image description here

Upvotes: 5

Views: 4861

Answers (2)

PunitD
PunitD

Reputation: 2333

TL;DR version:
Call inputName.setBackgroundResource(R.drawable.edittext_background);after every call to inputLayoutName.setError(getString(R.string.err_msg_name));

Longer version:
This happens because when you call setError(errormsg) on TextInputlayout internally(in TextInputLayout's code) this method after setting up your Errorview calls following method:
updateEditTextBackground();

Inside the updateEditTextBackground() method of TextInputLayout class, your editText's background color is changed using following line:

if (mErrorShown && mErrorView != null) {
        // Set a color filter of the error color
        editTextBackground.setColorFilter(
                AppCompatDrawableManager.getPorterDuffColorFilter(
                        mErrorView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
    }

As a result your Edittext's background color changes to red color (This is picked up from Errorview's default textAppearance style from the framework).

Only workaround possible in your case would be to add below line after all your setError methods in if clause:

inputName.setBackgroundResource(R.drawable.edittext_background);

validateName() method:

   private boolean validateName() {

    if (inputName.getText().toString().trim().isEmpty()) {
        inputLayoutName.setError(getString(R.string.err_msg_name));
        inputName.setBackgroundResource(R.drawable.edittext_background);
        requestFocus(inputName);
        return false;

    } else {
        inputLayoutName.setErrorEnabled(false);
    }

    return true;
}

Possibly only workaround to your problem at the moment.
Not sure if this is the appropriate way to handle custom Edittext background situation.

Upvotes: 0

KDeogharkar
KDeogharkar

Reputation: 10959

use theme instead of hintTextAppearance

 <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           app:hintTextAppearance="@color/colorPrimary"
            android:theme="@style/TextAppearance>

your style

 <style name="TextAppearance" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/splahbgcolor</item>
        <item name="android:textColorHint">@color/splahbgcolor</item>
    </style>

Upvotes: 0

Related Questions