I set color of my TextView randomly. I have array String[] colors = {"BLUE", "RED", "GREEN", "BLACK", "YELLOW", "GRAY"}. I need to check if color of my TextView in this array and return index. How can I do it?
Asked
Active
Viewed 61 times
1 Answers
0
First get the text (or background) color from your TextView
. Then simply iterate over your list of colors (not color names) and return the index when you find a match.
When you use List
instead of an array the indexOf()
function already does this for you.
int textColor = textView.getCurrentTextColor();
List<Integer> colors = Arrays.asList(Color.BLUE, Color.RED, Color.GREEN, Color.BLACK, Color.YELLOW, Color.GRAY);
int index = colors.indexOf(textColor);

Bram Stoker
- 1,202
- 11
- 14