android – Assigning text color to text in EditText-ThrowExceptions

Exception or error:

How do I assign a different color to text in EditText by extending it?

How to solve:

use spans:

TextView textView = (TextView)findViewById(R.id.mytextview01);
Spannable WordtoSpan = new SpannableString("partial colored text");        
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(WordtoSpan);

###

you can change the text color by adding android:textColor like this:

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#f00" />

###

I know this is a very old question but since this was the first I got while searching, I would like to add an answer so that future readers get them.

We can create our own custom style in styles.xml file with many other attributes in addition to textColor and apply to our EditText.

Add this in styles.xml,

<style name="MyEditTextstyle">
    <item name="android:textColor">@color/dark_blue</item>
    <item name="android:background">@drawable/my_custom_edittext_bg</item>
    <item name="android:layout_marginTop">5dp</item>
    <item name="android:layout_marginBottom">5dp</item>
    //many more per requirement
</style>

And then simply apply this style to the EditText as,

<EditText
    style="@style/MyEditTextstyle" //here
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:hint="Item Name"
    android:padding="10dp" >
    <requestFocus />
</EditText>

Simple, right. 🙂 Important part is we can apply the same stuff to any more EditText’s if needed. 🙂

###

To change it programatically, add a color in your colors.xml file

colors.xml:

<resources>
    <color name="colorText">#424242</color>

and reference it like this:

myEditText.setTextColor(ContextCompat.getColor(this, R.color.colorText));

###

I had problems with using textColor attribute in style.xml file:

        <item name="android:textColor">@color/white</item>

For me editTextColor works :

        <item name="android:editTextColor">@color/white</item>

###

From XML Layout :

We can change EditText text color by adding android:textColor as below :

<EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Your Name"
    android:textColor="#FF0000"
</EditText>

From Java Class :

We can change EditText text color pragmatically by adding method EditText.setTextColor() as below :

EditText myEditText = (EditText)findViewById(R.id.myEditText);
myEditText.setTextColor(Color.RED);

###

android:textColor, set this property in EditText xml.

###

<EditText
  android:layout_width="what_ever_size"
  android:layout_height="what_ever_size"
  android:textColor="@color/your_choice_of_color" />

Leave a Reply

Your email address will not be published. Required fields are marked *