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.
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.
Comments