Android add linearLayout to tableRow programmatically

11,331

You might have a look at this SO answer, which explains how you can programmatically create a TableRow, inflating it from a partial XML definition.

EDIT, after comments: The gist of linked answer is:

  • Create a partial layout representing the table row, with its own style set:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"
  style="@style/tr_withborder" />
  • Dynamically inflate the partial via code:
TableRow tr = (TableRow) getLayoutInflater().inflate(R.layout.partial_table_row, null);
Share:
11,331
Ste
Author by

Ste

Updated on June 05, 2022

Comments

  • Ste
    Ste almost 2 years

    I'm populating a tableLayout programmatically. I want that my row are like this

    <TableRow android:id="@+id/tableRow1"
                            android:layout_width="wrap_content" android:layout_height="wrap_content">
                            <ImageView android:src="@drawable/icon" android:id="@+id/imageView1"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
                            <LinearLayout android:id="@+id/linearLayout2"
                                android:layout_width="wrap_content" android:layout_height="wrap_content"
                                android:orientation="vertical">
                                <TextView android:id="@+id/textView2" android:text="TextView"
                                    android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
                                <TextView android:text="TextView" android:id="@+id/textView1"
                                    android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
                            </LinearLayout>
                            <Button android:text="Button" android:id="@+id/button1"
                                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
                        </TableRow>
    

    I've tried it by code doing this

    TableRow row = new TableRow(activity);
          row.setGravity(Gravity.CENTER);
          row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
    
    
          image.setPadding(10, 10, 10, 10);       
          ((LinearLayout) image).setGravity(Gravity.LEFT);
          row.addView(image);
    
          LinearLayout main = new LinearLayout(row.getContext());
         main.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
          main.setOrientation(LinearLayout.VERTICAL);
    
    
          TextView nameView = new TextView(activity);
    
          if(name.length() > 22)
              nameView.setText(name.substring(0, 22)+"...");
          else
              nameView.setText(name);
    
          nameView.setPadding(10, 10, 10, 10);
          nameView.setTextColor(Color.parseColor("#83be56"));
          nameView.setTypeface(null,Typeface.BOLD);
          nameView.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
    
          main.addView(nameView,new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
    
          row.addView(main);
    
          bt.setText("Respond");
          bt.setPadding(10,10,10,10);
          bt.setGravity(Gravity.CENTER);
    
          row.addView(bt);
    

    But the linearlayout doesn't appear in the row. Where i'm going wrong?? thanks