How to add another EditText when I click and fill in another (Android)

I was looking for time to search, but cannot find a way to add another EditText after I click on it.

I am trying to describe my problem in this image:

enter image description here

Is there any container that provides this functionality?

+4
source share
2 answers

Check this:

public class YourActivity extends Activity {
    private LinearLayout holder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_activity);
        //get a reference to the LinearLayout - the holder of our views
        holder = (LinearLayout) findViewById(R.id.holder);
        addNewEdit();
    }

    private void addNewEdit() {
        //inflate a new EditText from the layout
        final EditText newEdit = (EditText) getLayoutInflater().inflate(R.layout.new_edit, holder, false);
        //add it to the holder
        holder.addView(newEdit);
        //set the text change lisnter
        newEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //here we decide if we have to add a new EditText view or to
                //remove the current
                if (s.length() == 0 && holder.getChildCount() > 1) {
                    holder.removeView(newEdit);
                } else if (s.length() > 0 && ((before + start) == 0)) {
                    addNewEdit();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}

your_activity.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/holder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
</LinearLayout>

And new_edit.xml:

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

</EditText>

EDIT: Of course, you should set the correct paddings / margin and perhaps create your own and stylized layouts for the holder and the items you are inflating.

+1
source

5 XML android:visibility="gone" , . TextWatcher , .

et1.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                et2.setVisibility(TextUtils.isEmpty(s.toString()) ? View.GONE : View.VISIBLE);
                // The rest of them also???
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

, EditTexts, ViewGroups (RelativeLayout, LinearLayout, FrameLayout,...), addView

+2

Source: https://habr.com/ru/post/1584365/


All Articles