Search code examples
javaandroidtextview

Is it possible to add a setOnColorChangeListener for a TextView in Android?


For example I would like to print a Toast -->COLOR CHANGED<-- every time I change the textColor of TextView programmatically. Is this possible?


Solution

  • Possibly you add a callback in the same method in which you are setting the new color to your text view.

    Example

    public void setNewTextColor(int color) {
       yourTextView.setTextColor(color);
       yourCallbackMethod();
    }
    
    public void yourCallbackMethod() {
       //you can do whatever in this method
    }
    

    Update - You can add your custom textview and define your callback by overriding the setTextColor method inside your custom textview class.

    Official guide to create a custom view in android - https://developer.android.com/training/custom-views/create-view