-4

I created a 10x10 board on a TableLayout filled with TextViews skin Java Class. Soon after, I tried to let all 100 TextViews Clickable , but only the TextView the last column and last line is clickable. I wonder how do I get all TextViews be clickable.

EDIT: Put the onClick inside the loop, so all TextViews were clickable, however only the last TextView changes color, regardless of what was clicked. Someone would have a solution?

public class GameAct extends AppCompatActivity {

    TableLayout tableLayout;
    TableRow tableRow;
    TextView textView;
    Chronometer chronometer;
    long tempoQuandoParado = 0;
    boolean isClickPause = false;


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

    chronometer = (Chronometer)findViewById(R.id.chronometer);
    tableLayout = (TableLayout) findViewById(R.id.tabuleiro);

    GradientDrawable gd = new GradientDrawable();
    gd.setStroke(2, 0xFFFFFFFF);
    gd.setColor(Color.rgb(0, 0, 128));

    final GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setStroke(2, 0xFFFFFFFF);
    gradientDrawable.setColor(Color.RED);

    tableLayout = (TableLayout) findViewById(R.id.tabuleiro);

    for (int i = 0; i < 10; i++){

        tableRow= new TableRow(this);
        tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

        for (int j = 0; j < 10; j++) {

            textView = new TextView(this);
            textView.setBackgroundDrawable(gd);
            textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    textView.setBackgroundDrawable(gradientDrawable);

                    if (isClickPause) {
                        chronometer.setBase(SystemClock.elapsedRealtime() + tempoQuandoParado);
                        chronometer.start();
                        isClickPause = false;
                    } else {
                        chronometer.setBase(SystemClock.elapsedRealtime());
                        chronometer.start();
                        tempoQuandoParado = 0;
                    }
                }
            });

            tableRow.addView(textView,35, 35);
        }

        tableLayout.addView(tableRow, new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    }

}

}

sebenalern
  • 2,515
  • 3
  • 26
  • 36

1 Answers1

1

Add Click listener on text view inside the loop

for (int i = 0; i < 10; i++){
      tableRow= new TableRow(this);
      tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
      for (int j = 0; j < 10; j++) {
        textView = new TextView(this);
        textView.setBackgroundDrawable(gd);
        textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    v.setBackgroundDrawable(gradientDrawable);

                    if (isClickPause) {
                        chronometer.setBase(SystemClock.elapsedRealtime() + tempoQuandoParado);
                        chronometer.start();
                        isClickPause = false;
                    } else {
                        chronometer.setBase(SystemClock.elapsedRealtime());
                        chronometer.start();
                        tempoQuandoParado = 0;
                    }
                }
         });
        tableRow.addView(textView,35, 35);
      }

      tableLayout.addView(tableRow, new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46