3

I'm trying to adding TableRows to a TableLayout, which is working fantastically. However, I'm running into some problems with layout parameters. The spacing between the TextViews within a TableRow isn't working out like I thought it would.

My current code looks as follows.

paymentTable = (TableLayout) findViewById(R.id.paymentTable);

for(i = 0; i < cursor.getCount(); i++) {

        TableRow row = new TableRow(this);

        TextView payAmount = new TextView(this);
        payAmount.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_AMOUNT)));
        payAmount.setPadding(payAmount.getPaddingLeft(), 
                             payAmount.getPaddingTop(), 
                             textView.getPaddingRight(), 
                             payAmount.getPaddingBottom());

        TextView payDate = new TextView(this);
        payDate.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_DATE)));

        row.addView(payDate);
        row.addView(payAmount);

        paymentTable.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        cursor.moveToNext();
    }

Afterwards, the TableLayout looks something along the lines of:

January$500

February$500

March$405

But I want it to look like:

January    $500
February   $500
March      $405

To clarify things, I want each new TableRow and the TextViews it contains to inherit layout properties of an existing TableRow and TextView(s).

Jason L
  • 1,812
  • 2
  • 22
  • 43

2 Answers2

1

You need to add a RelativeLayout to each Table

TableRow row = new TableRow(this);
                            row.setId(tag);
                         // Creating a new RelativeLayout

                            RelativeLayout relativeLayout = new RelativeLayout(this);
                            // as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams
                            TableRow.LayoutParams rlp = new TableRow.LayoutParams(
                                    TableRow.LayoutParams.MATCH_PARENT,
                                    TableRow.LayoutParams.MATCH_PARENT);   
                            rlp.setMargins(0, 0, 0, 0);
                            row.addView(relativeLayout, rlp);

Then you add your TextView to the RelativeLayout. Like this

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                          RelativeLayout.LayoutParams.WRAP_CONTENT,
                          RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payAmount = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_LEFT);
payAmount.setLayoutParams(lp);
relativeLayout.addView(payAmount);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                          RelativeLayout.LayoutParams.WRAP_CONTENT,
                          RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payDate = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
payDate.setLayoutParams(lp);
relativeLayout.addView(payDate);

This is the way I handle positioning interface components within a table row.

David
  • 111
  • 1
  • 5
  • how can i set orientation of relative layout i m adding textview inside a loop and then adding all textview to relative layout but all these textviews are added horizontally i m newbie please let me know how can i achieve my target? – nida Nov 18 '13 at 07:33
1

yawus this is a good tutorial will help you set the table layout http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/

this is the xml layout

http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/

this is the activity code

http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/8/

TableRow tableRow= new TableRow(this);

            ArrayList<Object> row = data.get(position);

            TextView idText = new TextView(this);
            idText.setText(row.get(0).toString());
            tableRow.addView(idText);

            TextView textOne = new TextView(this);
            textOne.setText(row.get(1).toString());
            tableRow.addView(textOne);

            TextView textTwo = new TextView(this);
            textTwo.setText(row.get(2).toString());
            tableRow.addView(textTwo);

            dataTable.addView(tableRow);
Avi Kumar
  • 4,403
  • 8
  • 36
  • 67
  • I decided to download his code and run it for myself. However, it seems that he runs into the same problem myself. While the minimum width he set for the columns helps, the columns for 'TextFieldOne' and 'TextFieldTwo' still run next together without spacing. – Jason L Mar 04 '12 at 01:39
  • look at this if you have the same issue then you can refer if not then we could try something else http://stackoverflow.com/questions/6660849/problem-to-display-dynamic-rows-in-table-layout-android/6661680#6661680 – Avi Kumar Mar 04 '12 at 14:30
  • to adjust padding at run time you can use tableRow.setPadding(5, 0, 0, 0); – Avi Kumar Mar 04 '12 at 14:36
  • Yawus are you using some background image for every cell of table – Avi Kumar Mar 20 '12 at 10:43
  • @Yawus if you use a background image then use an image with border would be a better option – Avi Kumar Mar 21 '12 at 13:31
  • Sadly the links aren't working anymore, you should think to put the code directly into stackoverflow so we can find the solution as well. – Lumy Jan 15 '17 at 11:41