Solution 1 :
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
Solution 2 :
AFAIK, there is no documented, official way to achieve what you want.
But I may suggest a workaround for this, although it’s some kind of overkill. I vouch for this answer for flexibility since it can fit for almost any use cases that involved with TextView usage.
You can create a custom TextView by extending TextView class, and add a custom interface in your custom TextView class that can be used to apply a color change listener. Like this:
public class MyCustomTextView extends TextView {
// other code, but you not need it since it's already inherited from the parent; unless you want to customize them too.
public interface OnColorChangeListener {
void onColorChanged()
}
private OnColorChangeListener onColorChangeListener;
// a public method to apply a color change listener interface to your TextView
public void setOnColorChangeListener(OnColorChangeListener onColorChangeListener) {
this.onColorChangeListener = onColorChangeListener;
}
public void setTextColor() {
// REMEMBER & BE AWARE: this is the original TextView method for setting a color.
// here you can call the listener onColorChanged() method.
onColorChangeListener.onColorChanged()
}
}
The next step for you is to change your TextView within the XML layout file to this custom TextView class. Then, in your activity/fragment where you want to listen for color changes, you can do it simply like this:
yourCustomTextView.setOnColorChangeListener(new MyCustomTextView.OnColorChangeListener() {
void onColorChanged() {
Toast.makeText(context, "Your text", Toast.LENGTH_SHORT).show();
}
});
With approach like this, every time you change the color of your custom TextView the toast will be shown.
Hope this helps. Feel free to comment if you do not understand/want to suggest.
Happy coding!
Problem :
For example I would like to print a Toast
–>COLOR CHANGED
<– every time I change the textColor
of TextView
programmatically. Is this possible?
Comments
Comment posted by stackoverflow.com/questions/8292712/…
I have seen this post.
Comment posted by F.Mysir
The change of color comes externally and it is random in my case…
Comment posted by developer.android.com/training/custom-views/create-view
You can either use a default TextView and use the above method or you can create a entire custom textview and override the setTextColor and register you callback method inside it (So using this way you have to use your custom textview rather than using the default one). So in your custom view, that callback will be called everytime when setTextColor is called. Please refer to this guide for creating custom view —
Comment posted by FEBRYAN ASA PERDANA
@F.Mysir see my answer for information on how to achieve what you want using custom TextView
Comment posted by F.Mysir
Hi, thank you! It looks like I need something like this! But I can not make it work! While I start typing yourCustomTextView.setOnColo[…] setOnColor…. becomes red. It looks like it can not find it.
Comment posted by FEBRYAN ASA PERDANA
Hi @F.Mysir , did you properly follow the instruction I set above? Have you changed your TextView from Android-default TextView class to your custom TextView class?