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

Jignesh Jain
LinearLayout linContact = (LinearLayout) mView.findViewById(R.id.linContacts);

LinearLayout.LayoutParams leftGravityparas = new LinearLayout.LayoutParams(0,LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams rightGravityParams = new LinearLayout.LayoutParams(30, 30);

for (int i = 0; i < contactList.size(); i++) {
    final ClsAdviserData contact = .contactList.get(i);

    if (contact.isSelected()) {

        linearLayout = new LinearLayout(getActivity());
        LinearLayout.LayoutParams linMainparam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        linearLayout.setBackgroundColor(getActivity().getResources().getColor(R.color.light_grey_backgeound));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setLayoutParams(linMainparam);
        linMainparam.setMargins(0, 10, 0, 0);

        leftGravityparas.gravity = Gravity.LEFT;
        leftGravityparas.weight = 0.9f;
        TextView txtContact = new TextView(getActivity());
        txtContact.setTextSize(16);
        // txtContact.setBackgroundColor(getActivity().getResources().getColor(R.color.light_grey_backgeound));
        txtContact.setLayoutParams(leftGravityparas);
        txtContact.setId(i);

        leftGravityparas.setMargins(0, 10, 0, 0);

        txtContact.setPadding(20, 10, 10, 10);
        txtContact.setText(contact.getName());

        linearLayout.addView(txtContact, leftGravityparas);

        rightGravityParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
        rightGravityParams.weight = 0.1f;

        final ImageView imgDelContact = new ImageView(getActivity());

        imgDelContact.setLayoutParams(rightGravityParams);
        imgDelContact.setTag(i);
        imgDelContact.setClickable(true);
        imgDelContact.setOnClickListener(this);
        imgDelContact.setImageResource(R.drawable.ic_close_grey);

        linearLayout.addView(imgDelContact, rightGravityParams);
        // linContact.setTag(i);
        linContact.addView(linearLayout);

        imgDelContact.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Toast ==>" + contact.getName() + v.getTag(), Toast.LENGTH_SHORT).show();

                // linContact.removeViewAt((Integer) v.getTag());
                linearLayout.setVisibility(View.GONE);

                // lin.removeViewAt((Integer)v.getTag());
            }
        });
    }
}

I wrote the above code to create the textfields and buttons dynamically; But now I need to remove 2 textfields and a button when the button is clicked. How do I do that?

Archit Jain

adding -
After initializing add subview using addView() method declared in LinearLayout

linearLayout.addView(txtContact);
linearLayout.addView(imgDelContact);

Hide -
To hide View ,so that you can get it again whenever required

imgDelContact.setVisibility(View.GONE);
txtContact.setVisibility(View.GONE);

Remove -
Or you can remove if you don't want to use it again.

linearLayout.removeView(txtContact);
linearLayout.removeView(imgDelContact);

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 add images from a string-array of urls to an activity's scrollview linear layout dynamically

From Dev

Dynamically add element to layout

From Dev

How can I dynamically add / remove servlets within a context?

From Dev

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

From Dev

Dynamically add/remove element

From Dev

how can i add textview under my image in linear layout programmatically?

From Dev

how can I add and remove class of a element whenever it is clicked

From Dev

How can I add and remove an active class to an element in pure JavaScript

From Dev

how can I add and remove class of a element whenever it is clicked

From Dev

How can I dynamically disable popover from a DOM element?

From Dev

How can i programmatically add a view from XML to a Layout?

From Dev

How to add the layout dynamically

From Dev

How can I remove content like &nbsp; from an element?

From Dev

How can I remove an attribute from an element in angular?

From Java

How can I remove an element from a list, with lodash?

From Dev

How can I get and remove the first element from a data structure?

From Dev

How can i remove bullet from dragable element?

From Dev

How can i remove from element the hover effect when clicked?

From Dev

How can I remove a randomly chosen element from a linked list?

From Dev

How can I select or remove specific element from vector in r?

From Dev

How can I remove specific element from a list?

From Dev

How can I remove the matching element from the array in mongo db?

From Dev

How can I remove an element from an array completely?

From Dev

How to add relative Layout below some element dynamically

From Dev

How can I distribute views in a linear layout (which is a child of another linear layout) evenly?

From Dev

How can I distribute views in a linear layout (which is a child of another linear layout) evenly?

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

From Dev

How I can add an element?

Related Related

  1. 1

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

  2. 2

    Dynamically add element to layout

  3. 3

    How can I dynamically add / remove servlets within a context?

  4. 4

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

  5. 5

    Dynamically add/remove element

  6. 6

    how can i add textview under my image in linear layout programmatically?

  7. 7

    how can I add and remove class of a element whenever it is clicked

  8. 8

    How can I add and remove an active class to an element in pure JavaScript

  9. 9

    how can I add and remove class of a element whenever it is clicked

  10. 10

    How can I dynamically disable popover from a DOM element?

  11. 11

    How can i programmatically add a view from XML to a Layout?

  12. 12

    How to add the layout dynamically

  13. 13

    How can I remove content like &nbsp; from an element?

  14. 14

    How can I remove an attribute from an element in angular?

  15. 15

    How can I remove an element from a list, with lodash?

  16. 16

    How can I get and remove the first element from a data structure?

  17. 17

    How can i remove bullet from dragable element?

  18. 18

    How can i remove from element the hover effect when clicked?

  19. 19

    How can I remove a randomly chosen element from a linked list?

  20. 20

    How can I select or remove specific element from vector in r?

  21. 21

    How can I remove specific element from a list?

  22. 22

    How can I remove the matching element from the array in mongo db?

  23. 23

    How can I remove an element from an array completely?

  24. 24

    How to add relative Layout below some element dynamically

  25. 25

    How can I distribute views in a linear layout (which is a child of another linear layout) evenly?

  26. 26

    How can I distribute views in a linear layout (which is a child of another linear layout) evenly?

  27. 27

    Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

  28. 28

    Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

  29. 29

    How I can add an element?

HotTag

Archive