1

i am newbie and facing difficulty to achieve my required output. XML CODE: //this code is inside of ScrollView

 <LinearLayout 
        android:id="@+id/statusSecond_Layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
         <TableLayout 
             android:id="@+id/statusDisciplineTable_layout"
             android:layout_height="wrap_content"
             android:layout_width="fill_parent"

             ></TableLayout>

    </LinearLayout>

JAVA CODE:

setContentView(R.layout.status_view);
statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout);
for(int i=0;i<2;i++)
{
    TableRow statusTableRow = new TableRow(this);
    statusTableRow.setId(i);
    statusTableRow.setOrientation(TableRow.VERTICAL);
    TextView productsTextView = new TextView(this);
    productsTextView.setText("product name:"+i);
    statusTableRow.addView(productsTextView);
    //statusTableRow.setBackgroundColor(Color.BLACK);


    for(int j=0;j<2;j++)
    {
        RelativeLayout statusRelativelayout = new RelativeLayout(this);
        TableRow.LayoutParams rlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        rl.setMargins(0, 0, 16, 0);
        rl.addRule(RelativeLayout.ALIGN_LEFT);
            TextView label = new TextView(this);
        label.setId(j);
        label.setText("abcd:"+j);
        label.setLayoutParams(rl);
    statusRelativelayout.addView(label);
    statusTableRow.addView(statusRelativelayout,rlp);   

    }
    statusTableLayout.addView(statusTableRow);}

code output

please tell me what should i need to do changes in my current code to product required given image required outout

nida
  • 656
  • 3
  • 16
  • 38

1 Answers1

1

You can use LinearLayout instead of TableRow like this

TableLayout statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout);
        for(int i=0;i<2;i++)
        {
            LinearLayout statusTableRow = new LinearLayout(this);
            statusTableRow.setId(i);
            statusTableRow.setOrientation(LinearLayout.VERTICAL);
            TextView productsTextView = new TextView(this);
            productsTextView.setText("product name:"+i);
            statusTableRow.addView(productsTextView);
            //statusTableRow.setBackgroundColor(Color.BLACK);


            for(int j=0;j<2;j++)
            {
                RelativeLayout statusRelativelayout = new RelativeLayout(this);

                RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
                rl.setMargins(0, 0, 16, 0);
                rl.addRule(RelativeLayout.ALIGN_LEFT);
                    TextView label = new TextView(this);
                label.setId(j);
                label.setText("abcd:"+j);
                label.setLayoutParams(rl);
            statusRelativelayout.addView(label);
            statusTableRow.addView(statusRelativelayout);   

            }
            statusTableLayout.addView(statusTableRow);}

also put everything inside Relativelayout and then put inside ScrollView like this

<?xml version="1.0" encoding="utf-8"?>
<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
<LinearLayout 
        android:id="@+id/statusSecond_Layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
         <TableLayout 
             android:id="@+id/statusDisciplineTable_layout"
             android:layout_height="wrap_content"
             android:layout_width="fill_parent"

             >

             <LinearLayout
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content" >
             </LinearLayout>

         </TableLayout>

    </LinearLayout>

</RelativeLayout>

</ScrollView>
krishna
  • 4,069
  • 2
  • 29
  • 56
  • please see this LOC:IS IT RIGHT? TableRow.LayoutParams rlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT); – nida Nov 18 '13 at 08:48
  • TableRow.LayoutParams rlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT); --------------- have changed with this LOC its working fine ----------------------------------------------------------------- LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); – nida Nov 18 '13 at 09:01
  • because now there is no TableRow there is only LinearLayout which acts as a parent of dynamic Relative Layout – nida Nov 18 '13 at 09:03
  • you can just delete that line and change `statusTableRow.addView(statusRelativelayout,rlp)` to this `statusTableRow.addView(statusRelativelayout)` – krishna Nov 18 '13 at 09:07
  • I have also edited it in my answer,i am getting the o/p which you was asking even with that line – krishna Nov 18 '13 at 09:08
  • how can i apply margin property to linaerlayout and to textview both dynamically – nida Nov 18 '13 at 09:09
  • you cannot add it directly, but you can add through layoutparams – krishna Nov 18 '13 at 09:13
  • you can also check it http://stackoverflow.com/questions/2481455/set-margins-in-a-linearlayout-programmatically – krishna Nov 18 '13 at 09:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41401/discussion-between-user1668447-and-krishna) – nida Nov 18 '13 at 10:22