How to add the layout dynamically

anand

I want to add the layout dynamically. Actually in my app i am making comment section. The view for comment i have made in the LinearLayout and inside that i have added the ImageView(UserPics), Edittext and Post button.

XML for comments is like this:

<LinearLayout
    android:id="@+id/commment_section"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dip"
    android:orientation="vertical"
    android:layout_below="@+id/nmd_user_img"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    >

        <ImageView
            android:id="@+id/cmt_user_image"
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:layout_margin="5dip"
            android:padding="5dip"
            android:background="@color/layout_bg"/>
        <EditText
            android:id="@+id/cmt_user_edt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/cmt_user_image"
            android:layout_marginLeft="10dip"
            android:padding="5dip"
            android:hint="Enter your comments"/>
        <ImageButton
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="5dip"
            android:layout_marginRight="15dip"/>

    </RelativeLayout>
</LinearLayout>

Now, since multiple user can add the comments . I want as soon as a user enters the comment a new layout like this should get added dynamically below the previous comment.

varun bhardwaj

You should make a xml view of comment row and inflate it runtime and add it to the parent View.

Let say comment.xml is the view of comment row to be added dynamically so to inflate it follow below:

LayoutInflater inflator = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        View commentView = inflator.inflate(R.layout.your_xml, null);
        commentView.setId(commentID);
        yourLinearLayout.addView(commentView);

You can set anything in the comment row using the view object and calling findViewById. In this way no layout preparation code is in java file that make it really simple like we use to do in getView of adapter :)

If you got stuck anywhere in the steps please let me know.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to dynamically add ImageView to layout using Adapter

From Dev

How to add this type of layout dynamically (android)

From Dev

How to add 2 fragments dynamically in a single layout?

From Dev

Dynamically add layout to another layout

From Dev

Dynamically add element to layout

From Dev

Dynamically add QWebEngineView to layout

From Dev

Android: How to dynamically add a "Custom View" into a linear layout

From Dev

how can i add and remove element dynamically from linear layout?

From Dev

How to add relative Layout below some element dynamically

From Dev

how to add controls dynamically to table layout panel from panel

From Dev

how to add to layout dynamically with multiple RelativeLayout.LayoutParams

From Dev

How do I add a LinearLayoutCompat view to an existing xml layout dynamically?

From Dev

Add a TextView dynamically in a layout ANDROID

From Dev

Add a layout dynamically, to overlapp the other layout

From Dev

Dynamically add Layout after some layout?

From Dev

How to add row dynamically in table layout or How to show full db Records in single table Layout

From Dev

How to dynamically insert a relative layout into a table layout

From Dev

Dynamically add content to layout in MVC4

From Dev

How to change adapter layout dynamically?

From Dev

How to dynamically include a layout in Android?

From Dev

How to change the page layout dynamically

From Dev

How to open dynamically created layout?

From Dev

How do I dynamically add buttons using Java in a given XML layout file in android studio?

From Dev

How do I dynamically add items to ExtJS 4.2.1 border layout region containers?

From Dev

How to add images from a string-array of urls to an activity's scrollview linear layout dynamically

From Dev

How do I add buttons that are dynamically created in pure python to a kivy layout that is Written in Kivy Language?

From Dev

How do I dynamically add TextViews to my layout based on the content of a JSON file?

From Dev

Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

From Dev

Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

Related Related

  1. 1

    How to dynamically add ImageView to layout using Adapter

  2. 2

    How to add this type of layout dynamically (android)

  3. 3

    How to add 2 fragments dynamically in a single layout?

  4. 4

    Dynamically add layout to another layout

  5. 5

    Dynamically add element to layout

  6. 6

    Dynamically add QWebEngineView to layout

  7. 7

    Android: How to dynamically add a "Custom View" into a linear layout

  8. 8

    how can i add and remove element dynamically from linear layout?

  9. 9

    How to add relative Layout below some element dynamically

  10. 10

    how to add controls dynamically to table layout panel from panel

  11. 11

    how to add to layout dynamically with multiple RelativeLayout.LayoutParams

  12. 12

    How do I add a LinearLayoutCompat view to an existing xml layout dynamically?

  13. 13

    Add a TextView dynamically in a layout ANDROID

  14. 14

    Add a layout dynamically, to overlapp the other layout

  15. 15

    Dynamically add Layout after some layout?

  16. 16

    How to add row dynamically in table layout or How to show full db Records in single table Layout

  17. 17

    How to dynamically insert a relative layout into a table layout

  18. 18

    Dynamically add content to layout in MVC4

  19. 19

    How to change adapter layout dynamically?

  20. 20

    How to dynamically include a layout in Android?

  21. 21

    How to change the page layout dynamically

  22. 22

    How to open dynamically created layout?

  23. 23

    How do I dynamically add buttons using Java in a given XML layout file in android studio?

  24. 24

    How do I dynamically add items to ExtJS 4.2.1 border layout region containers?

  25. 25

    How to add images from a string-array of urls to an activity's scrollview linear layout dynamically

  26. 26

    How do I add buttons that are dynamically created in pure python to a kivy layout that is Written in Kivy Language?

  27. 27

    How do I dynamically add TextViews to my layout based on the content of a JSON file?

  28. 28

    Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

  29. 29

    Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

HotTag

Archive