Adding the below layout code dynamically in android activity

96 views Asked by At

I have implemented a layout statically in Android layouts folder xml file which is as follows

            <TableRow
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                >
                <LinearLayout
                    android:weightSum="1.0"
                    android:layout_weight=".75"
                    android:layout_height="match_parent"
                    android:layout_width="0dip"
                    android:background="#ffef1128"
                    >


                    <Button
                        android:id="@+id/textbox12"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.2"
                        android:layout_width="0dip"
                        android:textSize="14sp"
                        android:text="20%"
                        android:background="@color/fund_background_blue"
                        android:textColor="@color/gen_text_color"
                         />
                     </LinearLayout>

                <LinearLayout
                    android:layout_weight=".25"
                    android:layout_height="match_parent"
                    android:layout_width="0dip"
                    >


                    <TextView
                        android:id="@+id/TextView22"
                        android:text="12.4"
                        android:layout_weight=".15"
                        android:layout_height="wrap_content"
                        android:layout_width="0dip"
                        android:textColor="#000000"
                        android:gravity="right"
                        android:layout_gravity="center_vertical" />


                     <ImageView
                        android:src="@drawable/ic_up"
                        android:layout_height="15dp"
                        android:layout_width="0dip"
                        android:layout_weight=".05"
                        android:layout_gravity="center_vertical" />
                </LinearLayout>
            </TableRow>

How can i add these programatically in Activity instead of xml Layout.as it involves layout weight and weight sum.

sample output for reference

enter image description here

1

There are 1 answers

1
Khashayar Ghamati On

you can get instance from all widget and set Layout programmatically like this

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
 LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    layoutParams.weight=1;

    your_widget.setLayoutParams(layoutParams);