Custom style EditText using appcompat v7

I created a custom view extending the EditText and specified an attribute style to change the background shade 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 the attribute style:

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

      

I am using Theme.AppCompat in my application. 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 color works fine, however I cannot do the same with the custom editText.

I've tried this code, but it changes the overall state so that all states use the same color. ( stack overflow )

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

      

Is there a way to apply AppCompat styling on a custom view? Is this an AppCompat issue or have I done something wrong in my CustomEditText? Any idea would be much appreciated. Thank!

+3


source to share


2 answers


As of api 21, you have to subclass the AppCompatEdit text. The same is true for all supported widgets. Use AppCompat * for rendering and other supported features.



+5


source


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



AppCompat does not support rendering widgets for custom views.

0


source







All Articles