Franco Franco - 4 months ago 20
Java Question

How to get onClick event to repeat

I have some Java code that I want to act as a multiple choice trivia-style game. A random color string is displayed in a TextView and when the correct, corresponding color button is pressed, the random string in the TextView is updated. However, this only works once. On launching the activity, the only color button that can change the TextView string is the one that corresponds to the current string in the TextView. However, after this, the functions seems to "lock" and the corresponding color button of the first string is the only button that can make the TextView string change. Is there a way to reset the onClick events or have them loop? Many Thanks!!!!

public class MainActivity extends AppCompatActivity {

private static final Random r_generator = new Random();
String textViewString;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
TextView tv = (TextView) findViewById(R.id.color_text);
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
tv.setText(q);
textViewString = tv.getText().toString();


Button green= (Button) findViewById(R.id.green_button);
green.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Green")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);
}
});

Button blue = (Button) findViewById(R.id.blue_button);
blue.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Blue")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);
}
});

Button red = (Button) findViewById(R.id.red_button);
red.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Red")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);

}
});

Button yellow = (Button) findViewById(R.id.yellow_button);
yellow.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Yellow")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);

}
});

Button white = (Button) findViewById(R.id.white_button);
white.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("White")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);

}
});

Button orange = (Button) findViewById(R.id.orange_button);
orange.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Orange")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);

}
});

Button brown = (Button) findViewById(R.id.brown_button);
brown.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Brown")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);

}
});

Button pink = (Button) findViewById(R.id.pink_button);
pink.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Pink")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);

}
});

Button purple = (Button) findViewById(R.id.purple_button);
purple.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!textViewString.equals("Purple")) return;
Resources res = getResources();
String[] myString = res.getStringArray(R.array.colorArray);
String q = myString[r_generator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.color_text);
tv.setText(q);

}
});



}
}

Answer

So Franco, there are two things you can do to make your code more concise and to solve your problem. First, I recommend consolidating your onClick() functions to be one onClick(). First you need to update your XML code. In each Button add the following line

android:onClick="onClick"

This means that every button defined with this onClick function will execute the code in the onClick function when clicked. Your onClick function should look something similar to the following.

public void onClick(View view){
    //This casts your view to be a button so you can access its features as a button
    Button btn = (Button) view;
    textViewString = tv.getText().toString();

    if(!btn.getText().toString().equals(textViewString)){
         //Perform action to notify user that they pressed the wrong button
         //Do not return here. This is what caused your program to lock up
         //Maybe add a TextView that says correct/incorrect and you can change that when the user is correct or incorrect
    }

    //Gets a random color from your string array and sets it to the TextView
    q = myString[r_generator.nextInt(myString.length)];
    tv.setText(q);
}