1

Here is the EditText's abc_edit_text_material drawable XML:

<selector>
    <item android:state_enabled="false" android:drawable="@drawable/abc_textfield_default_mtrl_alpha"/>
    <item android:state_pressed="false" android:state_focused="false" android:drawable="@drawable/abc_textfield_default_mtrl_alpha"/>
    <item android:drawable="@drawable/abc_textfield_activated_mtrl_alpha"/>
</selector>

It defines a set of images but not colours (and I can not track the moment when the colour is applied).

The following piece of code makes the bottom line always stay red, though its thickness changes with focus (so, the same <selector> is still in use):

Drawable back = edittext.getBackground();
back.setColorFilter(0xffff0000, PorterDuff.Mode.SRC_ATOP);
editText.setBackground(back);

But if after that I restore the default drawable, colours start changing according to the current focus state (gray unfocused, accent focused):

back.setBackgroundResource(R.drawable.abc_edit_text_material);

The questions:

  1. Why is the same selector used after applying a modified Drawable?
  2. Why do colours start to respond to focus changes after re-setting the same XML drawable as the background? Is there some object storing a set of colours corresponding to selector's state_enabled, etc?
  3. (Maybe, the answer to this one will also make clear the previous two)
    At which moment and how is colour applied by default? I mean, does it check whether the background is default and which class calls for this colouring? I tried looking through the sources from AppCompatEditText to View and did not find anything like that
Alex
  • 1,165
  • 2
  • 9
  • 27

1 Answers1

1
  1. Why not? By getBackground() you retrieve a StateListDrawable, which seem to accept color filters.

  2. Responding to focus is the default behavior to make clear which item is currently focusing and where is your keyboard input going. You are not resetting the filtered background; you are pointing to the original one.

  3. Class stores its default background and applies filters to the unfocused (colorControlNormal) and focused (colorControlActivated) state. If you change it, well, it changes and filters are gone.

natario
  • 24,954
  • 17
  • 88
  • 158
  • I'm quite confused about the 2nd. `setBackgroundResource()` takes an arbitrary XML, makes a `Drawable` and passes it to `setBackgroundDrawable()` - so, it looks like any XML should get coloured as the default one. But even a copy of `abc_edit_text_material` stays black - only the original gets coloured. Why? There does not seem to be any check if `abc_edit_text_material` was passed as the `setBackgroundResource()` argument or not – Alex Aug 14 '15 at 12:02
  • 1
    Can't find sources right now, but I can assure it's checking for its own resources: default one will be tinted, others won't. That happens also, for example, for toolbar icons: back arrow and overflow menu will be tinted `colorControlNormal`, but only if they belong to the built-in set of appcompat icons. If you copypaste that selector in your own directory, maybe with another name, it won't be tinted. – natario Aug 14 '15 at 12:06
  • 1
    [here](http://stackoverflow.com/a/26817918/4288782) is the answer you are looking for. – natario Aug 14 '15 at 12:10