简体   繁体   中英

How to remove added views programmatically

I have a dialog wherein a user can add EditText and also remove it. I have successfully added EditText programmatically but my code for removing it doesn't work. I'm following this tutorial but in my case setup is inside a dialog.

and also i want to get all the the texts on those EditTexts and store it inside an Array.

This is my code:

 public void showSurveyDialog(Context context) {
        ImageButton btnAddChoices, btnRemoveChoice;
        Dialog dialog = new Dialog(context);

        dialog.setContentView(R.layout.survey_content);
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        btnAddChoices = dialog.findViewById(R.id.btn_add_choices);
        LinearLayout choiceLayout = dialog.findViewById(R.id.choice_layout);


        btnAddChoices.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View rowView = dialog.getLayoutInflater().inflate(R.layout.choice_item, null);
                // Add the new row before the add field button.
                choiceLayout.addView(rowView, choiceLayout.getChildCount() - 1);
                ImageButton imageButton = dialog.findViewById(R.id.btn_choice_close);
                imageButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("asdass","ASDASd");
                        choiceLayout.removeView((View)v.getParent());
                    }
                });
            }
        });

        dialog.show();

    }

When pressing btnAddChoices a layout with EditText and Button(for removing) is automatically added to a linear layout. I'am trying to make the remove button to work but it doesn't remove the view.

you can try my add and remove code for references, this can add or remove view programmatically:

private void addressField(View view){
        final int[] textCount = {0};
        LinearLayout addressLayout = view.findViewById(R.id.address_layout);
        addressScroll.removeView(addressLayout);
        View layout2 = LayoutInflater.from(view.getContext()).inflate(R.layout.address_text_field
                , addressLayout,false);
        layout2.setTag("address"+Integer.toString(counter));
        addressLayout.addView(layout2);
        addressScroll.addView(addressLayout);
        ImageView deleteButton = layout2.findViewWithTag("address"+Integer.toString(counter))
                .findViewById(R.id.delete_address);
        TextFieldBoxes addressText = layout2.findViewWithTag("address"+Integer.toString(counter))
                .findViewById(R.id.address_wrapper);
        addressText.setSimpleTextChangeWatcher((theNewText, isError) -> {
            if(Objects.equals(theNewText, "")){
                textCount[0] = 0;
                addressLayout.removeView(layout2);
            }

            if(theNewText.length() == 1 && textCount[0] == 0){
                addressField(view);
                ExtendedEditText addressText2 = layout2.findViewWithTag("address"+Integer.toString(counter-1))
                        .findViewById(R.id.address_text);
                addressText2.requestFocus();
                counter++;
            }

            textCount[0]++;
        });
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addressLayout.removeView(layout2);
            }
        });
    }

hope can solve your problems

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM