1

I have been using the code below that I got from another question to split into two lines to edit the color programmatically.

Changing tint color of Android EditText programmatically

((EditText) row1.getVirtualChildAt(i))

The below code is what I'm using after the above line to change the color.

.getBackground().mutate().setColorFilter(getResources().getColor(R.color.Green), PorterDuff.Mode.SRC_ATOP);

Right now it sets the underline color so its always green whether or not the EditText box is being used.

How can I set it so it goes back to the default color after I click away from the EditText box. I would also be fine with specifying another color as the default like a light gray color.

Community
  • 1
  • 1
Pie
  • 856
  • 4
  • 12
  • 33

3 Answers3

0

You can register your edit text with OnFocusChangeListener.java and on focus change you can change the color.

void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// color while typing
}else{
// color when clicked away
}

}
Ishan
  • 1,172
  • 10
  • 25
  • How can this work with using ((EditText) row1.getVirtualChildAt(i)).getBackground().mutate().setColorFilter(getResources().getColor(R.color.Green), PorterDuff.Mode.SRC_ATOP); It requires variable i to be declared as final as its in a loop. – Pie Nov 01 '16 at 22:39
0

You can create color selector xml for different edit text states that you want to add in the res/color/your_edittext_color_state.xml folder.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#YOUR_COLOR"/>
    <item android:state_focused="true" android:color="#ANY_COLOR"/>
    <item android:color="#DEFAULT_COLOR"/>
</selector>

Then you can set the foregroundTint in your code

editText.setForegroundTintList(getApplicationContext().getResources().getColorStateList(R.color.your_edittext_color_state));

Mansi Shah
  • 227
  • 2
  • 6
  • I created a folder called color and created a xml resource as described above but the below line dosn't seem to change anything. ((EditText) row1.getVirtualChildAt(i)).setForegroundTintList(getApplicationContext().getResources().getColorStateList(R.color.your_edittext_color_state)); Also getColorStateList is depreciated by the way. Is there something newer that would be better? – Pie Nov 01 '16 at 22:24
  • 1
    Did you change the color in the xml above? For ColorStateList you can refer to http://stackoverflow.com/questions/15543186/colorstatelist-android-programmatically – Mansi Shah Nov 01 '16 at 22:36
0

I ended up following the answer that was given here: How do I create ColorStateList programmatically?

I did change a few things around as order DOES matter and it took a moment to realize this.

Placed the below code block right at the beginning of MainActivity.

int[][] states = new int[][] {
        new int[] { android.R.attr.state_focused}, // enabled
        //new int[] {-android.R.attr.state_enabled}, // disabled
        //new int[] {-android.R.attr.state_checked}, // unchecked
        new int[] { android.R.attr.state_window_focused}  // pressed
};

int[] colors = new int[] {
        Color.GREEN,
        //Color.BLUE,
        //Color.YELLOW,
        Color.GRAY
};

ColorStateList myColorAccentList = new ColorStateList(states, colors);

Then placed my statement in my for loop where it was needed.

((EditText) row1.getVirtualChildAt(i)).setBackgroundTintList(myColorAccentList);

For anyone else you will probably just want to add this part at the end of your edittext.

.setBackgroundTintList(myColorAccentList);
Community
  • 1
  • 1
Pie
  • 856
  • 4
  • 12
  • 33