How to get textview text color?

In the given code, lbl [0] .getTextColor () gives an error, but I do not know how to get the color of the text textview in the java file, please help me.

public void angry(View v) { if (lbl[0].getTextColor() == Color.BLACK) lbl[0].setTextColor(Color.RED); if (lbl[0].getTextColor() == Color.RED) lbl[0].setTextColor(Color.BLACK); } 

Thank.

+52
android
Jul 19 2018-11-11T00:
source share
4 answers

Use this

textView.getCurrentTextColor()

+133
Jul 19 '11 at 11:03
source share

One important thing to keep in mind is getCurrentTextColor (), as well as similar methods like getCurrentHintTextColor () and getHighlightColor () return int value, not hex, which are mainly used to determine colors. This can be even more confusing, as this is a negative number, for example, for red it is -65536, for green -16711936 and for white -1.

To make this simple, this is because getCurrentTextColor () returns the difference between the current color and the white value (both in decimal) minus 1. Expression: CurrentColor- (WhiteColor + 1), where white is 16777215. Of course, for standard colors you you can use predefined constants like Color.GREEN or Color.MAGENTA, but knowing that you can effectively use getCurrentTextColor () for any colors.

You can learn even more about setting up and getting colors in Android at http://android4beginners.com/2013/07/lesson-1-3-how-to-change-a-color-of-text-and-background-in -textview /

+9
Jul 05 '13 at 11:36 on
source share

If you use the contextcompat library to set the color for new versions of android, you can get a visually different value than what was above. This test worked for me when I used the following to set the test color

view.setTextColor (ContextCompat.getColor (ctx, color));

  textColor =view.getCurrentTextColor(); CoreApp.debug("viewutils", "green color: "+textColor); assertThat(textColor, is(ContextCompat.getColor(mCtx, R.color.green))); 
0
Jan 18 '17 at 18:43 on
source share

You can get the color code from TextView.

 int color=tv.getCurrentTextColor(); String hexColor = String.format("#%06X", (0xFFFFFF & color)); 
0
Jan 22 '19 at 6:59
source share



All Articles