'How to change only one button changed color without using button ids

When the user enters the correct answer and presses the confirm button, we want the button created by the array to change the color of the level button permanently.

Array activity

    Intent intent = getIntent();
    theme=(Theme) intent.getSerializableExtra("level");

    themeList();
    GridLayout gridLayout= findViewById(R.id.gridlay);
    for (int i = 0; i < theme.problems.length; i++) {
       final Problem problem = theme.problems[i];
       final  GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
       final Button button = new Button(this);
       button.setText(i+1+"");
       button.setBackgroundResource(R.drawable.button_style);
       layoutParams.width=200;
       layoutParams.height=200;
       layoutParams.setMargins(35, 60, 35, 60 );
       button.setOnClickListener(view -> {
           Intent intent1 =new Intent(LvActivity.this, ProblemActivity.class);
           intent1.putExtra("problem", problem);
           startActivity(intent1);
       });
       gridLayout.addView(button, layoutParams);
   }

Activity in which the user responds

buttonCheck.setOnClickListener(view -> {
        if(problem.isCorrectAnswer(answer.getText().toString())){
            dialog.show();
            dialogButton.setOnClickListener(view1 -> {
                Intent intent = new Intent(ProblemActivity.this, LvActivity.class);
                startActivity(intent);
                finish();
            });
        }

It is necessary that after the user enters the correct answer and presses the confirmation button, the color of the button created by the array, with which the user entered the level, changes its color to always.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source