Search code examples
androidandroid-edittextcustomizationattrandroid-color

EditText text color is always white after applying ColorStateList


I have this problem: I have a custom EditText that has a theme, as done for other components, I created a class that extends this EditText and in the onDraw method I applied a ColorStateList, I can correctly apply the colors I set, but text turns white and I would instead set it in the ColorStateList, how can I do, I am attaching the override of the onDraw () method, which as said works for the other components.

@Override
protected void onDraw(Canvas canvas) {
    if (!AppApplication.IS_DEFAULT) {
        ColorStateList thumbStates = new ColorStateList(
                new int[][]{
                        new int[] { android.R.attr.state_enabled}, 
                        new int[] {-android.R.attr.state_enabled}, 
                        new int[] {-android.R.attr.state_checked}, 
                        new int[] { android.R.attr.state_pressed}  

                },
                new int[]{
                        Color.BLACK,
                        Color.RED,
                        Color.GREEN,
                        Color.BLUE
                }
        );
        this.setBackgroundTintList(thumbStates);
    }
}

Solution

  • I see here the following required corrections:

    1. Don't set background Here. it will probably start an infinite loop of redrawing.

    2. Call setBackgroundTintList from outside of the class, or at least in the constructor of the class (better the one with 2 parameters).

    And for your case you don't need to override the method onDraw(canvas), or at least if you still want to, follow the rules:

    – call super.onDraw(canvas) if you want the class to draw it as usual.

    – don't allocate memory in this method, which you do by creating this int arrays.