-1

I am developing an application in which i am showing a image followed by text followed by image again horizontally in table layout.

I am creating the table layout programmatically as:

for(i = 0; i < 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);
}

With the help of this i am getting the out put as image 1.

enter image description here

But i need the out put as image 2.

enter image description here

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.

Please suggest me what changes or steps i follow. Need your valuable suggestion.

Manoj Fegde
  • 4,786
  • 15
  • 50
  • 95

1 Answers1

2

You can set the color using setBackgroundColor method.

horizontalLine.setBackgroundColor(Color.RED);

Edit :

You can horizontal line of fixed length and in center.

  • Add the TableRow
  • Define layout weight for View
  • Add the View in TableRow
  • Add the tableRow in Table layout
View horizontalLine = new View(this);
// Set weight
TableRow.LayoutParams params =  new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 10,0.7f);
horizontalLine.setLayoutParams(params);

TableRow tr = new TableRow(this);  
TableLayout.LayoutParams tableRowParams= new TableLayout.LayoutParams
             (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);

// Set margin
int leftMargin=20;
int topMargin=2;
int rightMargin=20;
int bottomMargin=2;

tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);

// Add View in tr
tr.addView(horizontalLine);

//Add tr in Table
tableLayout.addView(tr);

You can see the output :

enter image description here

Hope it helps ツ

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74