mardi 3 mai 2016

Randomize color and get true value

I am working app, and need me create validChoice between random color and name color. Where have a button with random color and three choice for validChoice.

public class MainActivity extends AppCompatActivity {

private Button coloredButton;
private final int NB_CHOICES = 3;
private TextView proposition_1TextView, proposition_2TextView, proposition_3TextView;
private int validChoice;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    coloredButton = (Button) findViewById(R.id.button);
    proposition_1TextView = (TextView) findViewById(R.id.mainActivityproposition_1TextView);
    proposition_2TextView = (TextView) findViewById(R.id.mainActivityproposition_2TextView);
    proposition_3TextView = (TextView) findViewById(R.id.mainActivityproposition_3TextView);


    proposition_1TextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectChoice(1);

            coloredButton.setBackgroundColor(randomColor());
            proposition_1TextView.setBackgroundColor(randomColor());
            proposition_2TextView.setBackgroundColor(randomColor());
            proposition_3TextView.setBackgroundColor(randomColor());

            proposition_3TextView.setText(randomName());
            proposition_2TextView.setText(randomName());
            proposition_1TextView.setText(randomName());
            //this will change the button background color every
            // time the button is being clicked
        }
    });

    proposition_2TextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectChoice(2);

            coloredButton.setBackgroundColor(randomColor());
            proposition_1TextView.setBackgroundColor(randomColor());
            proposition_2TextView.setBackgroundColor(randomColor());
            proposition_3TextView.setBackgroundColor(randomColor());

           proposition_3TextView.setText(randomName());
            proposition_2TextView.setText(randomName());
            proposition_1TextView.setText(randomName());

            //this will change the button background color every
            // time the button is being clicked
        }
    });

    proposition_3TextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectChoice(3);

            coloredButton.setBackgroundColor(randomColor());
            proposition_1TextView.setBackgroundColor(randomColor());
            proposition_2TextView.setBackgroundColor(randomColor());
            proposition_3TextView.setBackgroundColor(randomColor());

         proposition_3TextView.setText(randomName());
            proposition_2TextView.setText(randomName());
            proposition_1TextView.setText(randomName());
            //this will change the button background color every
            // time the button is being clicked
        }
    });

    //the bellow line is one time when the app start
    coloredButton.setBackgroundColor(randomColor());
    proposition_1TextView.setBackgroundColor(randomColor());
    proposition_2TextView.setBackgroundColor(randomColor());
    proposition_3TextView.setBackgroundColor(randomColor());



   proposition_3TextView.setText(randomName());
    proposition_2TextView.setText(randomName());
    proposition_1TextView.setText(randomName());
}

public int randomColor() {
    int[] colors = getResources().getIntArray(R.array.randomColors);
    int totalColors = colors.length;
    return colors[new Random().nextInt(totalColors)];
}



public String randomName() {
    String[] name = getResources().getStringArray(R.array.randomName);
    int totalName = name.length;
    return name[new Random().nextInt(totalName)];
}






private void selectChoice(final int choice) {


    boolean correctChoice = (choice == validChoice);

    TextView choiceTextView = (choice == 1) ? proposition_1TextView : (choice == 2) ? proposition_2TextView : proposition_3TextView;

    validChoice = choice;

and colors.xml I dont know thats good way for resolve problem.

<!-- Random colors -->
<color name="red">#da4453</color>
<color name="orange">#e9573f</color>
<color name="yellow">#f6bb42</color>
<color name="green">#8cc152</color>
<color name="whitegreen">#37bc9b</color>
<color name="whiteblue">#3bafda</color>
<color name="blue">#4a89dc</color>
<color name="purple">#967adc</color>
<color name="pink">#d770ad</color>
<color name="black">#434a54</color>
<!-- Random colors Array-->
<integer-array name="randomColors">
    <item>@color/red</item>
    <item>@color/orange</item>
    <item>@color/yellow</item>
    <item>@color/green</item>
    <item>@color/whitegreen</item>
    <item>@color/whiteblue</item>
    <item>@color/blue</item>
    <item>@color/purple</item>
    <item>@color/pink</item>
    <item>@color/black</item>

</integer-array>

<string name="rand_01">red</string>
<string name="rand_02">orange</string>
<string name="rand_03">yellow</string>
<string name="rand_04">green</string>
<string name="rand_05">white green</string>
<string name="rand_06">white blue</string>
<string name="rand_07">blue</string>
<string name="rand_08">purple</string>
<string name="rand_09">pink</string>
<string name="rand_10">black</string>

<integer-array name="randomName">
    <item>@string/rand_01</item>
    <item>@string/rand_02</item>
    <item>@string/rand_03</item>
    <item>@string/rand_04</item>
    <item>@string/rand_05</item>
    <item>@string/rand_06</item>
    <item>@string/rand_07</item>
    <item>@string/rand_08</item>
    <item>@string/rand_09</item>
    <item>@string/rand_10</item>
</integer-array>




Aucun commentaire:

Enregistrer un commentaire