user813813
user813813

Reputation: 355

How do I add an onclicklistener to a tablerow?

Is it possible to add an onclicklistener to a tablerow in Android?

I am dynamically adding these rows, and want to be able to open a new screen when a user click on a different row.

Here is my code to add a row.

TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams= new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=20;
int topMargin=10;
int rightMargin=15;
int bottomMargin=20;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
TextView tmake=new TextView(this);
tmake.setText(Html.fromHtml("<H1>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + NewText + "</H1>" +  "<br />"));
tr.addView(tmake); 
View v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
tl.addView(v);

Upvotes: 0

Views: 5644

Answers (2)

sandhu
sandhu

Reputation: 305

 /************ if table row is dynamic then this method else method 2 is perfect************/
//if we want to applay listener on dynamic tablerow then use this
//sure that perfect 
 TablRowe tr = new TableRow(this);
tr.setClickable(true);
tr.setId(100);// if in loop then add 1 counter with 100 like (100+counter) at end count++ it
 tr.setOnClickListener(this);
 @Override
    public void onClick(View v) 
{
        switch (v.getId())
         {
         case 100: 
               Toast.makeText(getApplicationContext(), "100", Toast.LENGTH_SHORT).show();   
             break;

         case 101:
            Toast.makeText(getApplicationContext(), "101", Toast.LENGTH_SHORT).show();  
             break;
}
/************************** for simple like this ************************/
   TableRow row1 = (TableRow)findViewById(R.id.row1);
row1.setonClickListener(this);
public void onClick(View v)
{
switch (v.getId())
         {
         case R.id.row1: 
           // do work
             break;
          }        
}

Upvotes: 1

Alone89
Alone89

Reputation: 307

Like this ;-)

tableRow.setClickable(true);
tableRow.setOnClickListener(onClickListener);

Upvotes: 7

Related Questions