2

I want to show data in table layout. Number of data is dynamic, so I need to implement dynamic rows in table. For this purpose I want to show 4 TextView in a single row.

Problem

Currently, I am able to show data in table and with four TextView, but I want some spaces between all four EditText, but unable to do it.

Here is my code

for(ItemRow row : this.getRows()) {

                if(row != null) {
                    // delcare a new row
                    newRow = new TableRow(this);
                    newRow.setLayoutParams(new LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                    newRow.setPadding(0, 2, 0, 2);

                    column1 = new TextView(this);
                    column2 = new TextView(this);
                    column3 = new TextView(this);
                    column4 = new TextView(this);

                    column1.setTextColor(Color.BLACK);
                    column2.setTextColor(Color.BLACK);
                    column3.setTextColor(Color.BLACK);
                    column4.setTextColor(Color.BLACK);
                    column1.setSingleLine(false);
                    column2.setSingleLine(false);
                    column3.setSingleLine(false);
                    column4.setSingleLine(false);

                    if(row.getColumnOne() != null){
                        column1.setText(row.getColumnOne());
                    } 

                    if(row.getColumnTwo() != null) {
                        column2.setText(row.getColumnTwo());
                    }

                    if(row.getColumnThree() != null) {
                        column3.setText(row.getColumnThree());
                    }

                    if(row.getColumnFour() != null) {
                        column4.setText(row.getColumnFour());
                    }

                    //Now add a row 
                    newRow.addView(column1);
                    newRow.addView(column2);
                    newRow.addView(column3);
                    newRow.addView(column4);
                    //Add row to table
                    table.addView(newRow, new TableLayout.LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                }
            }

And here is my output :

enter image description here

Need

I need spaces between columns. How can I do it?

2 Answers 2

5

Set padding left 5dp for every textveiw widget like android:paddingLeft="5dp"

Sign up to request clarification or add additional context in comments.

Comments

2

You have to add paddind for columns too as:

column1.setPadding(5, 5, 5, 5);    
column2.setPadding(5, 5, 5, 5);    
column3.setPadding(5, 5, 5, 5);    
column4.setPadding(5, 5, 5, 5);

Edits: Then you can set layoutparams as follows:

column1.setLayoutParams(new LinearLayout.LayoutParams(60,90));

2 Comments

but how to set multi line in text view of custom row? Because texts in textview are very large and then row get to long with single line.
Then you can setLayoutParams for TextView like column1.setLayoutParams(new LinearLayout.LayoutParams(60,90));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.