How can I make an EditText (both the underline and android:hint) have a 50% transparency when there is no text inputted and it isn't clicked on, then change to 100% when it is either clicked on or has inputted text?
In addition, how can I change the color of the underline (not hint) to orange when the transparency is 100% (when it is clicked or text has been inputted)?
Use the following code
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// If has focus then change the alpha to 1
mEditText.setAlpha(1);
// Set orange color filter
mEditText.getBackground().setColorFilter(Color.argb(255, 255, 165, 0), PorterDuff
.Mode.SRC_IN);
} else {
// When focus is lost, check if some text has been entered.
// If not then set the alpha to 0.5f (50% transparent)
if (TextUtils.isEmpty(mEditText.getText().toString())) {
mEditText.setAlpha(0.5f);
// Clear the color filter
mEditText.getBackground().clearColorFilter();
}
}
mEditText.invalidate();
}
});
Related
I have multiple background colors of the keyboard in my app user can select any color for the keyboard background. The problem I am getting is when the user selects any color to apply on the keyboard it's working but the candidate views not change the color its color is the same as the first theme. When I rotate the app screen the candidate view color is also changed. Can you help me please change the candidate view color on runtime?
mCandidateView.setBackgroundResource( R.color.colorBlack);
#Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
super.onStartInputView(attribute, restarting);
if (modeReceived == MyConstants.Companion.getTHEME_KEYBOARD_MODE_DRAWABLE()) {
try {
int drawablePrefs =
sharedPreferences.getInt(MyConstants.Companion.getTHEME_KEYBOARD_DRAWABLE(), R.color.colorBlack);
if (mInputView != null) {
mInputView.setBackgroundResource(drawablePrefs);
mCandidateView.setBackgroundResource(drawablePrefs);
}
I'm working on an app that has full UI customization. If you look at a EditText wrapped in an TextInputLayout, the EditText's default text hint color and line color are the same as the color textPrimaryColor defined in styles. When the EditText receives focus however, the line becomes the color of the accent defined in styles, the hint goes through the floating hint animation and changes to the accent color. It's pretty easy to do with styles.xml and themes.xml, however, I can't quite get it programmatically.
The current method I have set up is this:
public static void setInputTextLayoutColor(final int accent, final int text, TextInputLayout textInputLayout, AppCompatEditText edit) {
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
AppCompatEditText editText = (AppCompatEditText) v;
editText.getBackground().clearColorFilter();
if(hasFocus) editText.getBackground().setColorFilter(accent, PorterDuff.Mode.SRC_IN);
else editText.getBackground().setColorFilter(text, PorterDuff.Mode.SRC_IN);
}
});
setCursorColor(edit, accent);
try {
Field field = textInputLayout.getClass().getDeclaredField("mFocusedTextColor");
field.setAccessible(true);
int[][] states = new int[][]{
new int[]{}
};
int[] colors = new int[]{
accent
};
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();
}
}
The problem with this method is that A) the hint text is colored immediately to the accent color, it doesn't color to the accent when focus is received B) The EditText line starts off with the right color, but when it receives focus, it colorizes to accent defined in styles, not the programatically set color.
This is the non-focused picture (in reference to the "Event name" field). Here, the color of "event name" should be gray / white (like the text"):
This is the focused picture, here, everything is right except the color of the line, which should be green as well.
Wasn't able to figure out dynamically changing the hint color, but I got the line color to change with a listener:
AppCompatEditText edit = (AppCompatEditText)findViewById(R.id.event_create_name_edit);
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
AppCompatEditText edit2 = (AppCompatEditText)v;
if(hasFocus) edit2.setSupportBackgroundTintList(ColorStateList.valueOf(rui.getAccent()));
else edit2.setSupportBackgroundTintList(ColorStateList.valueOf(rui.getText()));
}
});
When a user enters information in an EditText, and moves to the next EditText, the information is highlighted as shown below:
The code for this:
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setBackgroundColor(Color.WHITE);
((EditText) v).setTextColor(Color.BLACK);
} else {
//v.setBackgroundColor(Color.LTGRAY); //also works like this
((EditText) v).setBackgroundColor(Color.LTGRAY);
((EditText) v).setTextColor(Color.BLACK);
}
}
});
Which is called in the onCreate method like this:
edittext = (EditText) findViewById(R.id.editText1);
However, It would be much better if the background color only applied to the text itself, rather than the view, like this (from the gmail app):
Does anybody have any suggestions on how to apply the background color to the text only (not the whole EditText view) as above?
Thanks.
You can achieve what you want by using a BackgroundColorSpan. You can find more information here:
http://developer.android.com/reference/android/text/style/BackgroundColorSpan.html
To use spans you need to build a SpannableString which you can do using a SpannableStringBuilder:
http://developer.android.com/reference/android/text/SpannableStringBuilder.html
I have a edit text field. I want it to have a transparent background. But when the user tries to edit it, it should be with a different background say white color and text color as black. And when he is done with the edit of the edit text and moves to the next control to enter other values i want the background to be transparent again. Please let me know how this is possible in android. Thank you for the help and time.
EditText editText = (EditText)findViewById(R.id.edit1);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
EditText editText = (EditText)findViewById(R.id.edit1);
editText.setBackgroundColor(hasFocus ? Color.WHITE : Color.TRANSPARENT);
editText.setTextColor(hasFocus ? Color.BLACK : Color.GRAY);
}
});
implement method onFocusChabgeListener() like below.
textView.setOnFocusChangeListener(new OnFocusChangeListener(){
if(textView.hasFocuss){
//set textColor and background color
}
else
{
//reset it
}
});
I have View in which there are two text boxes, and the user can select text color from another view on the same screen (through dialog box).
So when the user changes color via dialog box, I am changing color of EditText text and its hint. But when there is some text is available in EditText after that user selects other color, then that text is coming in that color. But if I remove all that text then the color of HintText is that of the previous color.
For example, currently if I have red color in text box and the user selects green color so text is there in green color. But if I remove that text then hint text are coming in red even if I change hint color in code. This problem only comes when there is some text there. if it is blank and hint text is there then problem is not coming.
Simply add this in your layout for the EditText :
android:textColorHint="#FFFFFF"
Use this to change the hint color. -
editText.setHintTextColor(getResources().getColor(R.color.white));
Solution for your problem -
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3){
//do something
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//do something
}
#Override
public void afterTextChanged(Editable arg0) {
if(arg0.toString().length() <= 0) //check if length is equal to zero
tv.setHintTextColor(getResources().getColor(R.color.white));
}
});
Default Colors:
android:textColorHint="#android:color/holo_blue_dark"
For Color code:
android:textColorHint="#33b5e5"
Inside Layout Xml File We can Change Color of Hint.....
android:textColorHint="#android:color/*****"
you can replace * with color or color code.
Seems that EditText apply the hintTextColor only if the text is empty. So simple solution will be like this
Editable text = mEditText.getText();
mEditText.setText(null);
mEditText.setHintTextColor(color);
mEditText.setText(text);
If you have multiple fields, you can extend the EditText and write a method which executes this logic and use that method instead.
Programmatically in Java - At least API v14+
exampleEditText.setHintTextColor(getResources().getColor(R.color.your_color));
This is like default hint color, worked for me:
editText.setHintTextColor(Color.GRAY);
You could call editText.invalidate() after you reset the hint color. That could resolve your issue. Actually the SDK update the color in the same way.