title: Problems with the click event in a TableLayout
body: <p>Good Morning everyone,</p>
  
  <p>I'm facing yet another problem in my path of learning Android. I made a dynamic TableLayout with the content of an CSV file. I need that when I click/touch a row in the table, the color should change, and later with a click of a button get the content of the same line. Now I'm stucked with the first part and of course I have no clue about how to get the data of the row.</p>
  
  <p>I declared the table inside a LinearLayout that is also inside of a ScrollView in my layout with the following properties:</p>
  
  <pre><code>    &lt;ScrollView
      android:id="@+id/scrollMotors"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true"
      android:layout_alignParentBottom="true"
      android:layout_marginLeft="50dp"
      android:layout_marginRight="50dp"
      android:layout_marginTop="90dp"
      android:layout_marginBottom="50dp" &gt;
      &lt;LinearLayout
          android:id="@+id/layoutMotors"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" &gt;
          &lt;TableLayout
              android:id="@+id/tableMotors"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="center_vertical"
              android:clickable="true"
              android:focusable="true"
              android:focusableInTouchMode="true"
              android:gravity="center_vertical"
              android:stretchColumns="*" &gt;
  
          &lt;/TableLayout&gt;
      &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>After in my java code, I declared the creation of the line:</p>
  
  <pre><code>        //Initialization of my table
      my_tableMotors = (TableLayout) findViewById(R.id.tableMotors);
      //This is an ArrayList&lt;ArrayList&lt;String&gt;&gt; that contains the lines of the CSV file,
      //I use this variable as a dynamic Matrix because my CSV file can change its dimensions.
      m = valuesFile.size(); 
      for (n = 0 ; n &lt; m ; n++)
      {
          //Declaration and initialization of my rows
          final TableRow line = new TableRow(MotorActivity.this);
          //Setting the parameters of my row
          line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
          line.setFocusable(true);
          line.setFocusableInTouchMode(true);
          line.setClickable(true);
          //Initialization of my TextViews that are gonna be the content of each one of the rows in the dynamic TableLayout
          myCol1 = new TextView(MotorActivity.this);
          myCol2 = new TextView(MotorActivity.this);
          myCol3 = new TextView(MotorActivity.this);
          myCol4 = new TextView(MotorActivity.this);
          j = valuesFile.get(n).size();
          for (i = 0 ; i &lt; j ; i++)
          {
              switch(i)
              {
              case 0: 
                  if (n == 0)
                  {
                      myCol1.setText("Line");
                  }
                  else
                  {
                      myCol1.setText(valuesFile.get(n).get(i)); //Sets value for the column
                  }
                  line.addView(myCol1);
                  break;
              case 1:
                  myCol2.setText(valuesFile.get(n).get(i)); //Sets value for the column
                  line.addView(myCol2);
                  break;
              case 2:
                  myCol3.setText(valuesFile.get(n).get(i)); //Sets value for the column
                  line.addView(myCol3);
                  break;
              case 3:
                  myCol4.setText(valuesFile.get(n).get(i)); //I use this variable for some other purpose
                  break;
              }
          }
          my_tableMotors.addView(line);
      }
      my_tableMotors.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {onClickedRow(); }});
  }
  </code></pre>
  
  <p>From what I've seen and read here, the best is to use a setOnClickListener and that's what I did using a bit of two different answers that I found here:</p>
  
  <pre><code>    public void onClickedRow()
  {
      m = my_tableMotors.getChildCount();
      for (n = 0 ; n &lt; m ; n++)
      {
          if (my_tableMotors.getChildAt(n).hasFocus())
          {
              my_tableMotors.setBackgroundColor(myColor);
          }
      }
  }
  </code></pre>
  
  <p>Now I can't get any focus at the tableLayout at all, so please if you see something wrong in my code or if you know how to help me with this I would appreciate it a lot!!!!</p>
  
  <p>Many thanks in advance :).</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>I found the way to get the focus. I changed the method not to the whole TableLayout but only to the TableRow, so ended up as this:</p>
  
  <p>*<strong><em>Before</em>*</strong></p>
  
  <pre><code>        my_tableMotors = (TableLayout) findViewById(R.id.tableMotors);
      m = valuesFile.size(); 
      for (n = 0 ; n &lt; m ; n++)
      {
          //Declaration and initialization of my rows
          final TableRow line = new TableRow(MotorActivity.this);
          /*Other declarations*/
          j = valuesFile.get(n).size();
          for (i = 0 ; i &lt; j ; i++)
          {
              /*Code*/
          }
          my_tableMotors.addView(line);
      }
      my_tableMotors.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {onClickedRow(); }});
  }
  </code></pre>
  
  <p>*<strong><em>After</em>*</strong></p>
  
  <pre><code>    my_tableMotors = (TableLayout) findViewById(R.id.tableMotors);
  m = valuesFile.size();
  for (n = 0 ; n &lt; m ; n++)
  {
      //Declaration and initialization of my rows
          final TableRow line = new TableRow(MotorActivity.this);
      /*Other declarations*/
      j = valuesFile.get(n).size();
      for (i = 0 ; i &lt; j ; i++)
      {
          /*Code*/
      }
      line.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {onClickedRow(); }});
      my_tableMotors.addView(line);
  }
  </code></pre>
  
  <p>I also made the change on how to set the color of the line:</p>
  
  <p>*<strong><em>Before</em>*</strong></p>
  
  <pre><code>my_tableMotors.setBackgroundColor(myColor);
  </code></pre>
  
  <p>*<strong><em>After</em>*</strong></p>
  
  <pre><code>my_tableMotors.getChildAt(n).setBackgroundResource(R.drawable.myColor);
  </code></pre>
  
  <p>Now I'm busy finding out how to get the data from the TableRow. As soon as I get that solve or an answer from you I think I have my problem solved!!!</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>With the help of @Luksprog I could find an answer to my problem of retrieving the content!!! I did used the next code using his solution:</p>
  
  <pre><code>    public void onClickedRow()
  {
      TableRow clickedRow = new TableRow(MotorActivity.this);
      m = my_tableMotors.getChildCount();
      for (n = 1 ; n &lt; m ; n++)
      {
          if (my_tableMotors.getChildAt(n).isFocused())
          {
              my_tableMotors.getChildAt(n).setBackgroundResource(R.drawable.highlightTableRow);
              clickedRow = (TableRow) my_tableMotors.getChildAt(n);
              j = clickedRow.getChildCount();
              for (i = 0; i &lt; j ; i++)
              {
                  switch(i)
                  {
                  case 0:
                      myField1 = (TextView) clickedRow.getChildAt(i);
                      break;
                  case 1:
                      myField2 = (TextView) clickedRow.getChildAt(i);
                      break;
                  case 2:
                      myField3 = (TextView) clickedRow.getChildAt(i);
                      break;
                  }
              }
          }
      }
  }
  </code></pre>