gyan awasthi
gyan awasthi

Reputation: 51

cell click event on text (cell's text) of dynamically created table layout in android

where allValues is array list and i am creating 5 column table.So help me to implement text click event on each text of each cell.I searched and found click event of table row but by this way i am not able to implement cell click event in table layout.

TableLayout tl = (TableLayout) findViewById(R.id.tlparent);

            for (int j = 0, k = 0; j < allValues.size() / 5; j++) 
              {
                    final TableRow tableRow = new TableRow(this);
                    for (int y = 1; y <= 5; y++) 
                    {
                        TextView tv = new TextView(this);
                        tv.setText(allValues.get(k));
                        tableRow.addView(tv);
                        k++;
                    }
                    tl.removeView(tableRow);
                    tl.addView(TABROW);
                }

Upvotes: 2

Views: 201

Answers (1)

Oliver Metz
Oliver Metz

Reputation: 3758

Simply add an onClickListener to your TextView:

TextView tv = new TextView(this);
tv.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TextView tv = (TextView) v;
        String text = tv.getText().toString();
        // process your text
    }
});

If you want to pass your table "coordinates" (j,k,y, etc.) to your calling function, make additional final variables before setting the OnClickListener and use these inside the onClick method.

Upvotes: 1

Related Questions