begginer
begginer

Reputation: 83

How to create dynamic layout in android

i want to create a layout dynamically. I have tried myself but unable to get desired output. I am new in android, Here is my XML code.

          <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/box_sh1">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#f4f4f4"
                android:weightSum="100">

                <Linear Layout
                    android:layout_width="0dp"
                    android:layout_weight="75"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:id="@+id/yeardetail"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="April 2010 to June 2012"
                        android:layout_marginTop="5dp"
                        android:textColor="#666"
                        android:textSize="14sp"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="25"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:gravity="right">
                    <Button
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:layout_marginRight="5dp"
                        android:background="@drawable/ic_action_edit"/>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

here is a link that looks exactly i want

enter image description here

Upvotes: 0

Views: 1529

Answers (1)

Upendra Shah
Upendra Shah

Reputation: 2301

create a row_layout which you want to add at runtime.. than inflate row_layout to view and than add to liniearlayout of main activity

 LinearLayout llGetQuote = (LinearLayout) mview.findViewById(R.id.llGetQuote);

 LayoutInflater li = LayoutInflater.from(getActivity());
 View row = li.inflate(R.layout.row_layout, null);
 llGetQuote.addView(row);

For remove a view

((ViewGroup) row.getParent()).removeView(row);

For add Clicklistner

you can set tag like

imgDelete.setTag(i);
imgDelete.setOnClickListener(CheckInActivity.this);

//onclick switch case

case R.id.imgDelete: 
       //here you get pos of view 
        pos = (int) v.getTag(); 

        //remove view code
break;

Upvotes: 2

Related Questions