Programmatically changing underline color of EditText - android

I have an ordinary EditText field that I would like to programmatically change the underline color for.
<EditText
android:id="#+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
Other answers suggest changing the background color filter like so:
editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
However, I don't see any change when I run the app. Changing the background itself:
editText.setBackground(color)
changes the entire EditText to color - not what I want!
How do I programmatically change the underline color for an EditText, AppCompatEditText or TextInputEditText? I am using version 25.0.1 of the Support Library.

#degs's answer is correct. But just add one little note: the AppCompatEditText#setSupportBackgroundTintList is now annotated with #RestrictTo(LIBRARY_GROUP) which means:
Restrict usage to code within the same group of libraries
Instead use ViewCompat#setBackgroundTintList. So in your example, it should look like this:
ColorStateList colorStateList = ColorStateList.valueOf(color);
ViewCompat.setBackgroundTintList(editText, colorStateList);

You need to set the backgroundTintList (or supportBackgroundTintList) on the EditText to an instance of ColorStateList containing only the color you wish to change the tint to. An easy way to do this in a backwards-compatible way looks like this:
ColorStateList colorStateList = ColorStateList.valueOf(color);
editText.setSupportBackgroundTintList(colorStateList);
This will give the EditText the desired underline color.

Changing the color of EditText underline programmatically can be done using ColorFilter class and other ways but you will have an API level problem. The best way to resolve these type of issues is by using a 9-Patch image.
https://romannurik.github.io/AndroidAssetStudio/nine-patches.html
go here download a set of drawables and put them in your drawable folder and change the background of EditText programmatically( et_joker.setBackground(your_drawable);
) This will work irrespective to API levels.

I couldn't make it working with the above solution when the edit text is in focus.
I had to add colorControlActivated in the theme.
<style name="StyledTilEditTextTheme">
<item name="colorControlNormal">#color/greyLight</item>
<item name="colorControlActivated">#color/gray_ccc</item>
</style>
This worked for me

Related

EditText how to set colorControlActivated in Android 4.4 KitKat Api 19?

I'm trying to set the purple underline and cursor colors in an EditText to match green buttons in a dialog (see pic attached). I need to set the colors within the code, as different screens have different color schemes, all set within the code. The colorControlActivated was not introduced until Lollipop and is not available in KitKat, which we have to support.
I look through the question on Stack Overflow Set EditText cursor color, Tried using AppCompatEditText and learned about colorControlActivated property in this article: TextInputLayout proper Theming. I've not found anything that allows me to change the colors in and EditText on KitKat.
Thanks for you help.
Try this solution :
Instead of using EditTextView, change it to "android.support.v7.widget.AppCompatEditText" and then do something like this :
<android.support.v7.widget.AppCompatEditText
app:backgroundTint="#color/red" />
Make sure you are setting app:backgroundTint instead of android:backgroundTint

Changing BackgroundTint of a MaterialButton

I'm using the lasted support design : 28, alpha3.
I use using the "Theme.MaterialComponents.Light.NoActionBar" as the theme for my application and "MaterialButton" instead of a normal "Button" in my layouts.
I can set the BackgroundTind from the XML as normal but i can't change it via java.
I tried:
deliverSwitch.setBackgroundTintList(getResources().getColorStateList(R.color.colorYellow));
deliverSwitch.setSupportBackgroundTintList(getResources().getColorStateList(R.color.colorYellow));
but none of them worked... I also tried to clear the current tint by leaving the setBackgroundTintList null and it doesn't work either.
I couldn't get it working either. As a workaround I did the following: First you get the current background Drawable, then you tint it with the desired color and set the new background with setBackgroundDrawable for your Material Button.
Drawable background = materialButton.getBackground();
background.setTint(ContextCompat.getColor(getContext(), R.color.bg_button_primary));
materialButton.setBackgroundDrawable(background);
I hope that helps.

Setting #null for textColor dynamically in android

I need an EditText to appear as a TextView for a form that will change from being write to read only. I have found a useful snippet of code to do this in a layout:
<EditText android:id="#+id/title"
android:layout_width="fill_parent"
style="?android:attr/textViewStyle"
android:background="#null"
android:textColor="#null" />
I need to do this dynamically (not in the layout). I know that the background can be set to #null by using setBackgroundResource(null). Because setTextColor(int color) takes an int, I assume that a specific color must be selected. What is the correct color to choose that would be the equivalent to #null? A color from the default theme? Or is there a better way to do this?
You can create color in your string.xml like
<color name="transparent">#00000000</color>
Then you can assign it to like
txtTitle.setTextColor(getResources().getColor(R.color.transparent));
#00000000 is equal to null.
You can call these methods on any View to disable editing or clicking. It will effectively make the view read only. Set them to true to re-enable them.
view.setFocusable(false);
view.setClickable(false);
view.setLongClickable(false);

what style/attr/drawable for inverse background?

I try to make a ExpandableListView where the group headers are drawn inverse. There is no problem with changing the text color, size etc. via XML. I even found out how to use the system defaults like
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#android:style/TextAppearance.Medium.Inverse"
/>
But for the background color?! I don't understand why these styles don't include background color information?!
Of course I could use a direct color, but I look for good default background attributes or styles. Like "style/Background.For.TextAppearance.Medium.Inverse" ;-)
What would be a good solution? So that for the dark themed devices I get white/gray, and for the white themed I get black?
Or should I simply use R.color.background_light?
Greetings, Joerg
PS: First question here ;-) Thanx to all the people answering here the last months and years: You great people made it much more easier for me to find a re-entrance in programming after 12 years break ;-)
As you observe, the styles with "TextAppearance" in their name only affect the foreground text attributes of the view. They are appropriate for the android:textAppearance attribute. The styles with "Widget" in their names define all the UI properties and will work in a style attribute, but Android doesn't define a "Widget.TextView.Inverse" style.
When I wanted to display an console-like log as an inverse text view, I used the following XML:
<TextView
android:textAppearance="#style/TextAppearance.AppCompat.Small.Inverse"
android:background="?android:colorForeground"
... />
It uses the theme's foreground color as the background for the view. With a dark theme it displays dark text on white, and in a light theme it displays light text on black.

What is default color for text in textview?

I set the color to red , and after that I want to set the color again back to default, but I do not know what is default color, does anyone knows ?
Actually the color TextView is:
android:textColor="#android:color/tab_indicator_text"
or
#808080
You can save old color and then use it to restore the original value. Here is an example:
ColorStateList oldColors = textView.getTextColors(); //save original colors
textView.setTextColor(Color.RED);
....
textView.setTextColor(oldColors);//restore original colors
But in general default TextView text color is determined from current Theme applied to your Activity.
There are some default colors defined in android.R.color
int c = getResources().getColor(android.R.color.primary_text_dark);
Get these values from attributes:
int[] attrs = new int[] { android.R.attr.textColorSecondary };
TypedArray a = getTheme().obtainStyledAttributes(R.style.AppTheme, attrs);
DEFAULT_TEXT_COLOR = a.getColor(0, Color.RED);
a.recycle();
There are defaults in the theme that Android uses if you don't specifiy a text color. It may be different colors in various Android UIs (e.g. HTC Sense, Samsung TouchWiz, etc). Android has a _dark and _light theme, so the defaults are different for these (but nearly black in both of them in vanilla android). It is however good practice to define your primary text color yourself for to provide a consistent style throughout the devices.
In code:
getResources().getColor(android.R.color.primary_text_dark);
getResources().getColor(android.R.color.primary_text_light);
In xml:
android:color="#android:color/primary_text_dark"
android:color="#android:color/primary_text_light"
As reference in vanilla Android the dark theme text color is #060001 and the in the light theme it's #060003 since API v1. See the android style class here
I know it is old but according to my own theme editor with default light theme, default
textPrimaryColor = #000000
and
textColorPrimaryDark = #757575
I used a color picker on the textview and got this #757575
It may not be possible in all situations, but why not simply use the value of a different random TextView that exists in the same Activity and that carries the colour you are looking for?
txtOk.setTextColor(txtSomeOtherText.getCurrentTextColor());
The color of text inside a TextView is totally dependent on your theme.
The easiest way to know it:
Add a TextView to any xml file
Select the TextView
Click on Split view
Open the Attributes tab and scroll to the color section.
As you can see, according to my theme it is: #android:color/secondary_text_material_light
I believe the default color integer value is 16711935 (0x00FF00FF).
hey you can try this
ColorStateList colorStateList = textView.getTextColors();
String hexColor = String.format("#%06X", (0xFFFFFF & colorStateList.getDefaultColor()));
I found that android:textColor="#android:color/secondary_text_dark" provides a closer result to the default TextView color than android:textColor="#android:color/tab_indicator_text".
I suppose you have to switch between secondary_text_dark/light depending on the Theme you are using
You could use TextView.setTag/getTag to store original color before making changes. I would suggest to create an unique id resource in ids.xml to differentiate other tags if you have.
before setting to other colors:
if (textView.getTag(R.id.txt_default_color) == null) {
textView.setTag(R.id.txt_default_color, textView.currentTextColor)
}
Changing back:
textView.getTag(R.id.txt_default_color) as? Int then {
textView.setTextColor(this)
}
There are some default colours which get defined in the Themes of app. Below is the code snippet which you can use to get the current default color programmatically.
protected int getDefaultTextColor(){
TextView textView = new TextView(getContext());
return textView.getCurrentTextColor();
}
There is no default color. It means that every device can have own.

Categories

Resources