I am currently attempting to create a game based on the STROOP EFFECT.
Within the game a String is presented in a Textview. The user then has to choose the color of the String rather than the word.
I am trying to give the user two options to choose from (2 buttons):
- The string of what is displayed in the TextView
- A string representation of the color in the Textview
Currently my application works for the String but for the color it gives the int value of the color.
How can I convert this int value to a string for the user? I have seen an answer to a similar question here but it is not the same.
E.g of current output, note in the below I actually want the number to say "Blue" not -16776961 (color of the word):
I am currently unsuccessfully trying to do so using:
btn2.setText("" + colorsOnScreen[randColor].toString());
Below is the full code from my activity:
public class Stroop extends ActionBarActivity {
HashMap<String, Integer> colors = new HashMap<>();
// putting the strings and color vals of the hashmap to an array
Object stringOnScreen[];
Object colorsOnScreen[];
// declare vars
TextView color;
Button btn1;
Button btn2;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stroop);
setUpGame();
stringOnScreen = colors.keySet().toArray();
colorsOnScreen = colors.values().toArray();
setUpQuestion();
Log.d("Length", "Length string: " + stringOnScreen.length);
Log.d("Length", "Length color: " + colorsOnScreen.length);
}// oncreate end
public void setUpQuestion() {
int randString = new Random().nextInt(stringOnScreen.length);
int randColor = new Random().nextInt(colorsOnScreen.length);
Log.d("ranString", "randString: " + randString);
Log.d("rancolor", "randcolor: " + randColor);
// set the text of the string in textview for user to see
color.setText("" + stringOnScreen[randString]);
color.setTextColor((int) colorsOnScreen[randColor]);
btn1.setText("" + stringOnScreen[randString]); //Set btn1 to the string val
btn2.setText("" + colorsOnScreen[randColor].toString()); // set btn2 to the color of the String
}
public void setUpGame() {
// setting up the hashmap
colors.put("Green", Color.GREEN);
colors.put("Blue", Color.BLUE);
colors.put("Red", Color.RED);
colors.put("Yellow", Color.YELLOW);
colors.put("Black", Color.BLACK);
// setting up vars
color = (TextView) findViewById(R.id.tvStroopColor);
btn1 = (Button) findViewById(R.id.btnStroop1);
btn2 = (Button) findViewById(R.id.btnStroop2);
result = (TextView) findViewById(R.id.tvStroopResults);
}
}
EDIT:
trying unsuccessfully to compare the string value from button 2 to the color of the textView's text:
if(btn2.getText().equals(color.getCurrentTextColor())){
result.setText("Correct");
}