1

I am trying to set a divider for the table columns that are dynamically created. My xml for TableLayout:

<TableLayout
    android:id="@+id/tableEdit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@drawable/table_header_divider"/>

The part where I dynamically added the columns:

TableRow headerrow = new TableRow(this.getActivity());
addTableHeader("Type", 175, headerrow);
addTableHeader("Exp", 175, headerrow);
headerrow.setBackground(ContextCompat.getDrawable(this.getActivity(), R.drawable.selector_table_header));
headerrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tableEdit.addView(headerrow);

private void addTableHeader(String title, int colWidth, TableRow headerrow) {
   TextView textview = new TextView(this.getActivity());
   textview.setText(title);
   textview.setLayoutParams(new android.widget.TableRow.LayoutParams(colWidth, android.widget.TableRow.LayoutParams.WRAP_CONTENT));
   headerrow.addView(textview);
}

However, the divider image is not showing at all. Any ideas? Or is there any way to set the divider programatically? Thanks!

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
QWERTY
  • 2,303
  • 9
  • 44
  • 85

1 Answers1

1

Try this

private void addTableHeader(String title, int colWidth, TableRow headerrow) {

    TextView textview = new TextView(this);
    textview.setText(title);
    textview.setLayoutParams(new android.widget.TableRow.LayoutParams(colWidth, android.widget.TableRow.LayoutParams.WRAP_CONTENT));
    headerrow.addView(textview);

    View v = new View(this);
    v.setLayoutParams(new TableRow.LayoutParams(3, TableRow.LayoutParams.MATCH_PARENT));
    v.setBackgroundColor(Color.RED);
    headerrow.addView(v);
}

EDIT you can add a boolean flag to check weather you need to add divider or not

public class MyActivity extends AppCompatActivity {


    TableLayout tableEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        tableEdit = findViewById(R.id.tableEdit);

        TableRow headerrow = new TableRow(this);
        addTableHeader("Type", 175, headerrow, true);// send true if you want to add devider
        addTableHeader("Exp", 175, headerrow, false);// send false if you don't want to add devider
        headerrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

        tableEdit.addView(headerrow);


    }

    private void addTableHeader(String title, int colWidth, TableRow headerrow, boolean flag) {

        TextView textview = new TextView(this);
        textview.setText(title);
        textview.setLayoutParams(new android.widget.TableRow.LayoutParams(colWidth, android.widget.TableRow.LayoutParams.WRAP_CONTENT));
        headerrow.addView(textview);

        if (flag) {
            View v = new View(this);
            v.setLayoutParams(new TableRow.LayoutParams(3, TableRow.LayoutParams.MATCH_PARENT));
            v.setBackgroundColor(Color.RED);

             ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        params.leftMargin = 100;
        params.rightMargin = 200;

        v.setLayoutParams(params);
            headerrow.addView(v);

        }
    }

}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Cool thanks so much! But the divider line is too close to the next column as per the screenshot in the question. I tried to add paddingRight but it does not shift at all. Any ideas? – QWERTY Jul 23 '18 at 07:21
  • @hyperfkcb sorry i didn't understand, if you a new query please post n new question with more detail so other user can try to help you my friend – AskNilesh Jul 30 '18 at 05:53