Franco Franco - 4 months ago 9
Android Question

How to create extended if-then-else statement with buttons and strings

I am trying to create an activity where there are nine buttons of different colors. I want the buttons to randomly change the textstring displayed above them only when they are pressed along with their corresponding color textstring. However, when I run the app, only the green button changes the textstring and it changes it even when the textstring displays a color besides green. Am I doing something wrong in my if-then-else code? (I have not completed the code for the remaining six color buttons.) Many thanks!!!

package com.example.franco.sampleapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.res.Resources;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import java.lang.String;
import java.util.Random;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.color_text);

Button green= (Button) findViewById(R.id.green_button);
green.setOnClickListener(this);
Button blue = (Button) findViewById(R.id.blue_button);
blue.setOnClickListener(this);
Button red = (Button) findViewById(R.id.red_button);
red.setOnClickListener(this);
Button yellow= (Button) findViewById(R.id.yellow_button);
yellow.setOnClickListener(this);
Button white = (Button) findViewById(R.id.white_button);
white.setOnClickListener(this);
Button orange = (Button) findViewById(R.id.orange_button);
orange.setOnClickListener(this);
Button brown= (Button) findViewById(R.id.brown_button);
brown.setOnClickListener(this);
Button pink = (Button) findViewById(R.id.pink_button);
pink.setOnClickListener(this);
Button purple = (Button) findViewById(R.id.purple_button);
purple.setOnClickListener(this);
textViewString = tv.getText().toString();

}

@Override
public void onClick(View v) {

if (v.getId() == R.id.green_button && textViewString.equals("Green")) {

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

} else if (v.getId() == R.id.blue_button && textViewString.equals("Blue")) {

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

} else if (v.getId() == R.id.red_button && textViewString.equals("Red")){

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

Don't make your MainActivity an OnClickListener.

Instead, create anonymous OnClickListeners to add to each button:

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);
  }
});

etc.

Then there is no need for a long if/else chain.