问题
I use a TextInputLayout, I would want to programmatically set the hint text color and the floating label color if the input field is mandatory. Before moving to TextInputLayout I used to set the hint text color programmatically using the following
textField.setHintTextColor(Color.RED);
Can someone guide me on how to set the hint text color and the floating label color programmatically for a TextInputLayout.
In the screenshot attached i would want the hint text Address 1 to be in red when not focused and the on focus the floating label Address 1 should be in red.
回答1:
I changed focused color with reflection. Here's the snippet it may help someone.
private void setUpperHintColor(int color) {
try {
Field field = textInputLayout.getClass().getDeclaredField("mFocusedTextColor");
field.setAccessible(true);
int[][] states = new int[][]{
new int[]{}
};
int[] colors = new int[]{
color
};
ColorStateList myList = new ColorStateList(states, colors);
field.set(textInputLayout, myList);
Method method = textInputLayout.getClass().getDeclaredMethod("updateLabelState", boolean.class);
method.setAccessible(true);
method.invoke(textInputLayout, true);
} catch (Exception e) {
e.printStackTrace();
}
}
EDIT 2018-08-01:
If you are using design library v28.0.0 and later, fields had changed from mDefaultTextColor
to defaultHintTextColor
and from mFocusedTextColor
to focusedTextColor
.
Check decompiled class for other fields.
回答2:
Please, take a good look at the documentation here: TextInputLayout Methods
There is a method :
setHintTextAppearance(int resId)
Which takes a resource id that could be a style resource!
I would try this and see how it goes!
I hope it helps you!
回答3:
Normally TextInputLayout hint text color comes from app's colorAccent.
But if you want to change then you can use style for that.
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:theme="@style/TextLabel">
</android.support.design.widget.TextInputLayout>
@style
<style name="TextLabel" parent="TextAppearance.AppCompat">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">@color/Color Name</item>
<item name="android:textSize">20sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">@color/Color Name</item>
<item name="colorControlNormal">@color/Color Name</item>
<item name="colorControlActivated">@color/Color Name</item>
</style>
But if you want to add red color then how you can differentiate with error color means basic standard error have Red color.
textField.setHintTextColor(Color.RED); Can someone guide me on how to set the hint text color and the floating label color programmatically for a TextInputLayout.
setHintTextColor works for API 23+
回答4:
For Changing the both Focused Color
and Default Text Color
for TextInput Layout
private void setInputTextLayoutColor(int color, TextInputLayout textInputLayout) {
try {
Field field = textInputLayout.getClass().getDeclaredField("mFocusedTextColor");
field.setAccessible(true);
int[][] states = new int[][]{
new int[]{}
};
int[] colors = new int[]{
color
};
ColorStateList myList = new ColorStateList(states, colors);
field.set(textInputLayout, myList);
Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
fDefaultTextColor.setAccessible(true);
fDefaultTextColor.set(textInputLayout, myList);
Method method = textInputLayout.getClass().getDeclaredMethod("updateLabelState", boolean.class);
method.setAccessible(true);
method.invoke(textInputLayout, true);
} catch (Exception e) {
e.printStackTrace();
}
}
EDIT : To Change AppCompactEditText line color
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.
回答5:
With the Material Components library you can use:
In the layout:
<com.google.android.material.textfield.TextInputLayout
app:hintTextColor="@color/mycolor"
android:textColorHint="@color/text_input_hint_selector"
.../>
in a style:
<style name="..." parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<!-- The color of the label when it is collapsed and the text field is active -->
<item name="hintTextColor">?attr/colorPrimary</item>
<!-- The color of the label in all other text field states (such as resting and disabled) -->
<item name="android:textColorHint">@color/mtrl_indicator_text_color</item>
</style>
in the code:
// Sets the text color used by the hint in both the collapsed and expanded states
textInputLayout.setDefaultHintTextColor(...);
//Sets the collapsed hint text color
textInputLayout.setHintTextColor(....);
回答6:
Let me share my experience on this. I also tried all solutions given in every related question to this one. i.e. Change hint color of child widget to TextInputLayout
I am glad to share the answer to this question with a little detail.
All we need to know is:-
Adding below line to style of either
TextInputLayout
or its child widget, is not much a help.<item name="android:textColorHint">@color/white</item>
as it will use colorAccent whenever the focus is received/granted to editable widget.
The actual answer to this problem is to add that style line in Application's style tag, by which it will set hint color when that or any editable area is not in focus. (That is the point we miss everytime).
Please let me know if we have other information on this.
Thanks!
回答7:
I was able to get the FloatingLabel in red using the following
textInputLayout.setErrorEnabled(true);
textInputLayout.setError(" ");
来源:https://stackoverflow.com/questions/35683379/programmatically-set-textinputlayout-hint-text-color-and-floating-label-color