This tutorial is going to explain you about changing the default behaviour of a EditText
View i,e setError()
method. Changing the error text color is also a part of giving branding to the application and nowadays many popular applications do this. It is achieved by a utility class called SpannableStringBuilder
.
In this tutorial i will explain you how to change the text color of setError()
method Programatically by using SpannableStringBuilder. It is a 5 line code by which you can give your own error text color. Please go through the below 3 Steps.
Step 1: Define Error Color
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> ... ... <color name="textColor">#000000</color> <color name="errorColor">#002db3</color> ... ... </resources>
Step 2: Activity Layout
Add an EditText
in it.
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="customerror.devdeeds.com.customeerror.MainActivity"> <EditText android:id="@+id/editTextView" android:layout_width="180dp" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:textSize="20sp" android:hint="Enter Text Here" android:textColor="@color/textColor" /> </RelativeLayout>
Step 3: In MainActivity Class
Open your activity class and build a SpannableStringBuilder
with Foreground color, defined errorColor
, and set it to the EditText
. That’s it.
import android.os.Build; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.SpannableStringBuilder; import android.text.style.ForegroundColorSpan; import android.widget.EditText; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText editTextView = (EditText) findViewById(R.id.editTextView); //Your EditText int errorColor; final int version = Build.VERSION.SDK_INT; //Get the defined errorColor from color resource. if (version >= 23) { errorColor = ContextCompat.getColor(getApplicationContext(), R.color.errorColor); } else { errorColor = getResources().getColor(R.color.errorColor); } String errorString = "This field cannot be empty"; // Your custom error message. ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(errorColor); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(errorString); spannableStringBuilder.setSpan(foregroundColorSpan, 0, errorString.length(), 0); editTextView.setError(spannableStringBuilder); } }