Within my android application I am creating a game base on the Stroop effect.
In order for the user to select an answer for the game they click one of two buttons:
- Will have the string of the TextView as its text
- Will have the color of the String in the TextView as it's text
For example:
In order for the user to be correct they must choose the button with the color NOT the string.
I do not want these two buttons to always be in the same positions on the screen as it would become very predictable to the user. I.e. I want them to alter so that sometimes one is on the right, then left etc after each answer is given.
How can I do so?
Current code of Activity:
public class Stroop extends ActionBarActivity implements View.OnClickListener {
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();
btn2.setOnClickListener(this);
btn1.setOnClickListener(this);
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
//Note: uncomment below if the solution under doesnt work
//btn2.setText("" + colorsOnScreen[randColor].toString()); // set btn2 to the color of the String
setBtn2Text();
// //logic to see if answer is correct, currently commented out to try answer from SO
//
// if(btn2.getText().equals(convertColorIntToString(color.getCurrentTextColor()))){
//
// result.setText("Correct");
// }
//
// //trace code
// Log.d("colortrace", " " + convertColorIntToString(color.getCurrentTextColor()));
//trace to check SO method of logic is working
Log.d("bool", " " + checkForMatchBtn2(btn2));
}
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);
}
public void setBtn2Text(){
switch(color.getCurrentTextColor()){
case Color.GREEN:
btn2.setText("Green");
break;
case Color.RED:
btn2.setText("Red");
break;
case Color.BLUE:
btn2.setText("Blue");
break;
case Color.YELLOW:
btn2.setText("Yellow");
break;
case Color.BLACK:
btn2.setText("Black");
break;
}
}
public void onClick(View v){
if(v.getId() == R.id.btnStroop2){
if(checkForMatchBtn2(btn2))
result.setText("Correct!");
else
result.setText("Wrong!");
}
if(v.getId() == R.id.btnStroop1){
if(checkForMatchBtn1(btn1))
result.setText("Correct!");
else
result.setText("Wrong!");
}
}
public boolean checkForMatchBtn2(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;
else if(color.getCurrentTextColor() == Color.BLACK && btn2.getText().equals("Black"))
return true;
else if(color.getCurrentTextColor() == Color.YELLOW && btn2.getText().equals("Yellow"))
return true;
else if(color.getCurrentTextColor() == Color.BLUE && btn2.getText().equals("Blue"))
return true;
else
return false;
}
public boolean checkForMatchBtn1(Button btn1){
if(color.getCurrentTextColor() == Color.GREEN && btn1.getText().equals("Green"))
return true;
else if(color.getCurrentTextColor() == Color.RED && btn1.getText().equals("Red"))
return true;
else if(color.getCurrentTextColor() == Color.BLACK && btn1.getText().equals("Black"))
return true;
else if(color.getCurrentTextColor() == Color.YELLOW && btn1.getText().equals("Yellow"))
return true;
else if(color.getCurrentTextColor() == Color.BLUE && btn1.getText().equals("Blue"))
return true;
else
return false;
}
}
Corresponding XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvStroopColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="3dp"
android:text="meditation "
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnStroop1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text_to_set"
android:text=" " />
<Button
android:id="@+id/btnStroop2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text_to_set"
android:text=" " />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvStroopResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="3dp"
android:text=" results... "
android:textSize="25dp" />
</RelativeLayout>
</LinearLayout>