简体   繁体   中英

How to add edittext dynamically in android?

How to add edittext dynamically in adapter?

i have an adapter from which i am creating edittext for user to input data dynamically.

i am storing all the created edit text from Json received from server in a list. Now i even have to provide an extra edit text for user to add dynamic/custom data on button click?

How do i do it?

    ArrayList<EditText> list = new ArrayList<>();
    list.add(holder.editText);

I will first get the title/hint of edit text from user using an alert box like this:

addField.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context, R.style.AlertDialogStyle);
            alertDialog.setMessage("");


            final EditText input = new EditText(context);
            input.setGravity(Gravity.CENTER);
            input.setHint("Enter Title for custom field");

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            input.setLayoutParams(lp);
            alertDialog.setView(input);
            alertDialog.setIcon(R.drawable.logo);

            alertDialog.setPositiveButton("Set",
                    new DialogInterface.OnClickListener()
                    {
                        public void onClick(DialogInterface dialog, int which)
                        {
                            list.add(holder.editText); //**App Crashes while doing this**
                        }
                    });

            alertDialog.setCancelable(false).show();
        }
    });

The edittext is not added to list and the app crashes on clicking on SET, of alertBox

I think you must provide at least one editText for focus in XML. Then in a java, you can create editTexts and programmatically add params etc. This source code worked for me:

EditText editText = new EditText(context);
            editText.setLayoutParams(layoutParamsForEdittext);
            editText.setBackground(context.getResources().getDrawable(R.drawable.bg_rounded_grey_border));
            editText.setId(surveyAnswers.getId());
            editText.setPadding(8, 8, 8, 8);
            editText.setScrollContainer(true);
            editText.setTextSize(16);
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
            editText.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(10)});
            editText.setGravity(Gravity.TOP);
            editText.setHintTextColor(context.getResources().getColor(R.color.colorTextHint));
            editText.setHint(surveyAnswers.getText());
          
            linearLayoutShelfShareAnswer.addView(editText);

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