0

In my application i am using the following code for display content in table view. following code works fine for me but here it is displaying data with out borders but i want to display data with borders for each row and column also.

Here is the reference code i used:

public void init() {
            TableLayout stk = (TableLayout) findViewById(R.id.table_main);
            TableRow tbrow0 = new TableRow(this);
            TextView tv0 = new TextView(this);
            tv0.setText(" Sl.No ");
            tv0.setTextColor(Color.WHITE);
            tbrow0.addView(tv0);
            TextView tv1 = new TextView(this);
            tv1.setText(" Product ");
            tv1.setTextColor(Color.WHITE);
            tbrow0.addView(tv1);
            TextView tv2 = new TextView(this);
            tv2.setText(" Unit Price ");
            tv2.setTextColor(Color.WHITE);
            tbrow0.addView(tv2);
            TextView tv3 = new TextView(this);
            tv3.setText(" Stock Remaining ");
            tv3.setTextColor(Color.WHITE);
            tbrow0.addView(tv3);
            stk.addView(tbrow0);
            for (int i = 0; i < 25; i++) {
                TableRow tbrow = new TableRow(this);
                TextView t1v = new TextView(this);
                t1v.setText("" + i);
                t1v.setTextColor(Color.WHITE);
                t1v.setGravity(Gravity.CENTER);
                tbrow.addView(t1v);
                TextView t2v = new TextView(this);
                t2v.setText("Product " + i);
                t2v.setTextColor(Color.WHITE);
                t2v.setGravity(Gravity.CENTER);
                tbrow.addView(t2v);
                TextView t3v = new TextView(this);
                t3v.setText("Rs." + i);
                t3v.setTextColor(Color.WHITE);
                t3v.setGravity(Gravity.CENTER);
                tbrow.addView(t3v);
                TextView t4v = new TextView(this);
                t4v.setText("" + i * 15 / 32 * 10);
                t4v.setTextColor(Color.WHITE);
                t4v.setGravity(Gravity.CENTER);
                tbrow.addView(t4v);
                stk.addView(tbrow);
            }

        }

and i called this init() in my oncreate method as below

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        init();
    }

finally xml code:

<ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#3d455b"
        android:layout_alignParentLeft="true" >

        <HorizontalScrollView
            android:id="@+id/hscrll1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:id="@+id/RelativeLayout1"
                android:layout_width="fill_parent"
                android:layout_gravity="center"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TableLayout
                    android:id="@+id/table_main"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true" >
                </TableLayout>
            </RelativeLayout>
        </HorizontalScrollView>
    </ScrollView>

any one help me to add borders to the output.

Venki WAR
  • 1,997
  • 4
  • 25
  • 38
chanduuu
  • 19
  • 6
  • Possible duplicate of [How can I create a table with borders in Android?](https://stackoverflow.com/questions/2108456/how-can-i-create-a-table-with-borders-in-android) – sirius May 07 '18 at 11:29

1 Answers1

0

The easiest way to do is here. You can get a hint from here. As link says to give background to text view, you can apply it at run time too.

Rozina
  • 409
  • 3
  • 14
  • yes it will ok for fixed data but i want to set borders for dynamic data – chanduuu May 07 '18 at 12:23
  • i don't know how many rows i have. the rows will increase on time base and my response base – chanduuu May 07 '18 at 12:24
  • @chanduuu you should try this **t1v.setBackground(getResources().getDrawable(R.drawable.cellborder)** cellborder.xml ** ** – Rozina May 07 '18 at 12:30
  • @chanduuu If you want the border to whole table you can apply ** android:background="@drawable/cellborder"** to you **TableLayout** if you want border in your text view you can give background to **t1v.setBackground(getResources().getDrawable(R.drawable.cellborder)** – Rozina May 07 '18 at 12:39