Leem
Leem

Reputation: 18308

how to handle click event on each cell of a table layout

I have a table layout which is as simple as this one.

What I need is to allowed end user to click each cell of the table, and do something on each cell.

But, it seems android table layout only support row based on click event, no cell based on click event. How to get rid of this?

Upvotes: 5

Views: 17124

Answers (2)

crubio
crubio

Reputation: 6422

You need to handle the click events using android:clickable="true" and android:onClick="clickHandlerCell" in your XML layout definition file, in my case in a LinearLayout.

To identify wich cell was clicked you could tag the view of each cell using view.setTag(uniqueID) when you create it. In clickHandlerCell function use view.getTag() to get the identity of your cell.

Upvotes: 6

Venky
Venky

Reputation: 11107

Try this code for click in particular Table Row :

TableLayout contact_table = (TableLayout)findViewById(R.id.contact_table);
final View row=contact_table.getChildAt(i);
row.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v){
        // TODO Auto-generated method stub
        row_id=contact_table.indexOfChild(row);
    }
});

Upvotes: 2

Related Questions