0

Created a table layout programmatically .it looks like this.enter image description here

here i want to add horizontal line after each row,how to do it programmatically. sample code to create table layout

ScrollView scrollView = new ScrollView(this);
TableLayout resultLayout = new TableLayout(this);
resultLayout.setStretchAllColumns(true);
resultLayout.setShrinkAllColumns(true);
TableRow tablerowMostRecentVehicle = new TableRow(this);
tablerowMostRecentVehicle.setGravity(Gravity.CENTER_HORIZONTAL);

TextView textViewMostRecentVehicle = new TextView(this);
textViewMostRecentVehicle.setText("Most Recent Vehicle Details");
textViewMostRecentVehicle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
textViewMostRecentVehicle.setTypeface(Typeface.SERIF, Typeface.BOLD);
tablerowMostRecentVehicle.addView(textViewMostRecentVehicle);
// ...
resultLayout.addView(tablerowMostRecentVehicle);
resultLayout.addView(tableRowRegistrationMark);
resultLayout.addView(tableRowMakeModel);
resultLayout.addView(tableRowColour);
resultLayout.addView(tableRowChasisNo);
resultLayout.addView(tableRowDateofFirstUse);
resultLayout.addView(tableRowTypeofFuel);
// ...
flx
  • 14,146
  • 11
  • 55
  • 70

4 Answers4

1

Use this code.

View view = new View(this);
view.setBackgroundResource(R.drawable.line);
resultLayout.addView(view);

Add this code after every table row.

Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
1

I needed to add view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1)); to @dipali's answer:

View view = new View(this);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
view.setBackgroundColor(Color.GRAY);
tablerowMostRecentVehicle.addView(view);
User42
  • 970
  • 1
  • 16
  • 27
0
View view = new View(this);
view.setBackgroundColor(Color.GRAY);
tablerowMostRecentVehicle.addView(view);

I hope it's useful to you.

User42
  • 970
  • 1
  • 16
  • 27
dipali
  • 10,966
  • 5
  • 25
  • 51
0

R.drawable.row_border :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#ffffff" />
    <stroke android:width="3dp" android:color="#99cc00" />

</shape>

then:

tbrow.setBackgroundResource(R.drawable.row_borders);