title: Android: Draw a custom view programmatically
body: <p>I am developing an application in which i am showing a image followed by text followed by image again horizontally in table layout.</p>
  
  <p>I am creating the table layout programmatically as:</p>
  
  <pre><code>for(i = 0; i &lt; arrayList.size(); i++){
      /* Find Tablelayout defined in main.xml */
      TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
      tableLayout.setStretchAllColumns(true);
  
      /* Create a new row to be added. */
      TableRow tableRow = new TableRow(this);
      tableRow.setId(i);
      tableRow.setClickable(true);
      tableRow.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
              v.setBackgroundColor(Color.GRAY);
          }
      });
      tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
  
      /* Create a Button to be the row-content. */
      ImageView imageView1 = new ImageView(this);
      if(arrayList.get(i).getImage().equalsIgnoreCase("Y")){
      // setImage
      }
  
      imageView1.setPadding(5, 5, 5, 5);
      imageView1.setLayoutParams(new TableRow.LayoutParams(100, 100));
      tableRow.addView(imageView1);
  
      TextView textViewName = new TextView(this);
      textViewName.setText(arrayList.get(i).getName());
      textViewName.setPadding(5, 5, 5, 5);
      textViewName.setGravity(Gravity.CENTER_VERTICAL);
      textViewName.setTextSize(15);
      textViewName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT,7));
      tableRow.addView(textViewName);
  
      ImageView imageView2 = new ImageView(this);
      imageView2.setImageDrawable(getResources().getDrawable(R.drawable.icon));
      imageView2.setPadding(25, 25, 25, 25);
      imageView2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT,1));
      tableRow.addView(imageView2);           
  
      tableLayout.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
  
      View horizontalLine = new View(this);
      horizontalLine.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 1));
      horizontalLine.setBackgroundColor(Color.rgb(50, 50, 50));
      tableLayout.addView(horizontalLine);
  }
  </code></pre>
  
  <p>With the help of this i am getting the out put as image 1.</p>
  
  <p><img src="https://i.stack.imgur.com/vkySR.png" alt="enter image description here"></p>
  
  <p>But i need the out put as image 2.</p>
  
  <p><img src="https://i.stack.imgur.com/H2Rsm.png" alt="enter image description here"></p>
  
  <p>The difference between image 1 and 2 is the red line. I am getting the line with the help of drawing view but that covers whole width. I need a line which is as same as red in image 2. In center and of fix width.</p>
  
  <p>Please suggest me what changes or steps i follow. Need your valuable suggestion.</p>