0

How to change counter when is 33,66,99,elc ??

the project is here on githup https://github.com/hamza94max/Counter

any one can help me ?


public class MainActivity extends AppCompatActivity {
    TextView textView;
    private int counter = 0;

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

        textView=findViewById(R.id.countertext);
        reset=findViewById(R.id.reset);
        RelativeLayout relativeLayout=findViewById(R.id.reltivee);



        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                counter++;


                    textView.setText(Integer.toString(counter));
                    textView.setTextColor(Color.BLACK);


                }
        });


    }}

3 Answers 3

1

you can change color by using modulo like that

int currentColor = Color.BLACK;//this is activity level variable


if(counter%33 == 0){
    currentColor = getRandomColor()//implement getRandomColor() method
}
textView.setTextColor(currentColor)
6
  • I tried before but it changes only color of number 33 and I need change color between 33 and 66 ,66 and 99 ...elc Commented Apr 28, 2020 at 20:54
  • Actually this would change the color at 33, 66, 99 and all other multiples of 33. Commented Apr 28, 2020 at 21:34
  • I want to change color every 33 times from random colors , I know how to make a random but I want the condition or for loop Commented Apr 28, 2020 at 22:46
  • please look the last version of my answer I think it is correct. Every multiple of 33 you change the color of the text than you use this color until the counter's value hits to another multiple of 33
    – M.ekici
    Commented Apr 29, 2020 at 7:54
  • Yes , it is very useful Commented Apr 29, 2020 at 15:31
0

Do you mean like this?

if (counter>=33 && counter<66){
  //counter is bigger or equal than 33 AND smaller than 66
  //33, 34, ... 64, 65
  textView.setTextColor(color)
} else if (counter>=66 && counter<99) {
  textView.setTextColor(color)
} else if (counter>=99) {
  textView.setTextColor(color)
}
5
  • but I want every 33 times ? Commented Apr 28, 2020 at 22:16
  • If you want 33,66,99,... than @M.ekici 's answer is correct. Give us some examples. Commented Apr 28, 2020 at 22:25
  • @M.ekici solution change the only numbers 33,66,99 elc Commented Apr 28, 2020 at 22:42
  • I want to change color every 33 times from random colors , I know how to make a random but I want the condition or for loop Commented Apr 28, 2020 at 22:44
  • Do u know how ? Commented Apr 29, 2020 at 14:26
0

Just use a reminder % like that && add funcation for random color.

  relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                counter++;
               if (counter % 33 == 0) textView.setTextColor(getRandomColor);

                    textView.setText(Integer.toString(counter));
                }
        });

    private int getRandomcolor() {
        int[] colors = {Color.BLUE, Color.RED, Color.YELLOW}; // Add colors you want 
        int random = new Random().nextInt(colors.length);
        return colors[random];
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.