0

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):

  1. The string of what is displayed in the TextView
  2. 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):

enter image description here

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");
        }
Community
  • 1
  • 1
bigcoder995
  • 97
  • 1
  • 1
  • 7

1 Answers1

0

Do something like:

public void setBtn2Text(){
    switch(color.getCurrentTextColor()){
        case Color.GREEN:
            btn2.setText("Green");
        case Color.RED:
            btn2.setText("Red");
            break;
        // Continue for the other colors
    }
}

and then call setBtn2Text(); after you call setupQuestion(); in onCreate()

EDIT

To compare the button text string to the text view text color

public boolean checkForMatch(Button btn2){
    if(color.getCurrentTextColor() == Color.GREEN && btn2.getText().equals("Green"))
        return true;
    else if(color.getCurrentTextColor() == Color.RED && btn2.getText().equals("Red"))
        return true;
    // continue for the rest of the colors
    else 
        return false;
}

and do

public void onClick(View v){
    if(v.getId() == btn2.getId()){
        if(checkForMatch(btn2))
            result.setText("Correct!");
        else
            result.setText("Wrong!");
    }

    // Do what you need to for your other buttons
}

Of course there are other ways of doing this (for example utilizing a switch in onClick), but this is an example of a simple way you could do it.

zgc7009
  • 3,371
  • 5
  • 22
  • 34
  • Thank you for your answer, How then can I compare the String value in btn2 to the color of the text in color (text View)? I am currently trying to do it unsuccessfully as im comparing a string to an int. Please see my edit at the bottom? – bigcoder995 Jul 27 '14 at 23:46
  • Thank you you have been a great help – bigcoder995 Jul 27 '14 at 23:57