Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing tint color of Android EditText programmatically

Tags:

android

I am trying to change the tinting color of an EditText View programmatically during runtime. Basically i want to change what you would usually apply as ?attr/colorControlNormal like in the default background drawable.

Changing the background tint does not correctly apply by just setting a new ColorsStateList with one color:

editText.setBackgroundTintList( ColorStateList.valueOf( color ) );

For one the result is applied to all EditText although the tint list is applied and internally mutates the drawable. Also the alpha as specified in the default background 1 is visible at the beginning.

Here is the outcome of setting the tint color on just the first EditText:

outcome of setting the tint color on just the first EditText

So my question would be: How can I properly apply the tint programmatically to an EditText?

like image 655
Moritz Avatar asked Feb 03 '15 15:02

Moritz


People also ask

How to change EditText color in android programmatically?

You must create a custom EditText with a listener to change its own color. After that, when you want to change the color, you just have to call the listener and every single EditText will response this listener.

How do you remove tint color from Android?

imageView. drawable. setTintList(null) to clear the tint. Docs: Color state list to use for tinting this drawable, or null to clear the tint This value may be null.

What is the use of tint in Android?

Tint color means when we want to change the color of the image while rendering in ImageView. In XML is very easy to change tint color by just setting up the attribute android:tint="" in the ImageView tag, as shown in the following example.


2 Answers

This works for me:

editText.getBackground().setColorFilter(getResources().getColor(R.color.your_color),                                         PorterDuff.Mode.SRC_ATOP); 

Source: Changing EditText bottom line color with appcompat v7

like image 169
ingyesid Avatar answered Sep 24 '22 00:09

ingyesid


With the newly introduced android.support.v4.graphics.drawable.DrawableCompat#setTint setting the color is now possible.

like image 40
Moritz Avatar answered Sep 21 '22 00:09

Moritz