3

I created a custom view extending EditText and specified an attribute style to change the background tint color.

public class CustomEditText extends EditText {

    public CustomEditText (Context context) {
        this(context, null);
    }

    public CustomEditText (Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.customEditTextStyle);
    }

    public CustomEditText (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs,  R.attr.customEditTextStyle);
    }
    // Some other code...
}

Then I added an attribute style:

<resources>
    <attr name="customEditTextStyle" format="reference" />
<resources>

I am using Theme.AppCompat in my app. I am already overriding colorPrimary, colorPrimaryDark and colorAccent.

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/blue</item>
    <item name="colorPrimaryDark">@color/blue_dark</item>
    <item name="colorAccent">@color/blue_accent</item>
    <item name="customEditTextStyle">@style/CustomEditText</item>
</style>
<style name="CustomEditText" parent="Widget.AppCompat.EditText">
    <item name="colorPrimary">@color/blue</item>
    <item name="colorPrimaryDark">@color/blue_dark</item>
    <item name="colorAccent">@color/blue_accent</item>
</style>

The editText background colors are working fine, however, I can't do the same with the custom editText.

I have tried using this code but it changes the overall state, such that all states uses the same color.(https://stackoverflow.com/a/28433337)

editText.getBackground().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_ATOP);

Is there a way to apply AppCompat Style in Custom View? Is this AppCompat issue or I did something wrong in my CustomEditText? Any idea would be much appreciated. Thanks!

Community
  • 1
  • 1
theQwertiest
  • 86
  • 1
  • 9

2 Answers2

6

As of api 21 you must subclass AppCompatEdit text. Same is true for all the supported widgets. Use AppCompat* to get tinting and other back supported functionality.

Patrick Jackson
  • 18,766
  • 22
  • 81
  • 141
0

Solved the problem by extending android.support.v7.internal.widget.TintEditText.

AppCompat doesn't support widget tinting for custom views.

theQwertiest
  • 86
  • 1
  • 9