sunil_patidar
sunil_patidar

Reputation: 203

generate dynamic id for multiple edittext with button click

I have one row with EditText. My scenario is when user clicks on a button another row will be added. Somehow I have achieved this but both EditText have same id. So how to assign the id of EditText dynamically created. My EditText is in the layout XML file. Is it possible with XML or I have to create EditText programatically. Thanks in advance.

    private void inflateEditRow(String name) {

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.row, null);
    final ImageButton deleteButton = (ImageButton) rowView
            .findViewById(R.id.buttonDelete);
    final EditText editText = (EditText) rowView
            .findViewById(R.id.req);

    if (name != null && !name.isEmpty()) {
        editText.setText(name);
    } else {
        mExclusiveEmptyView = rowView;
        deleteButton.setVisibility(View.VISIBLE);
    }

    // A TextWatcher to control the visibility of the "Add new" button and
    // handle the exclusive empty view.
    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {

            if (s.toString().isEmpty()) {
                mAddButton.setVisibility(View.VISIBLE);
                deleteButton.setVisibility(View.VISIBLE);

                if (mExclusiveEmptyView != null
                        && mExclusiveEmptyView != rowView) {
                    mContainerView.removeView(mExclusiveEmptyView);
                }
                mExclusiveEmptyView = rowView;
            } else {

                if (mExclusiveEmptyView == rowView) {
                    mExclusiveEmptyView = null;
                }

                mAddButton.setVisibility(View.VISIBLE);
                deleteButton.setVisibility(View.VISIBLE);
            }
        }


    public void onAddNewClicked(View v) {
    // Inflate a new row and hide the button self.
    inflateEditRow(null);
    v.setVisibility(View.VISIBLE);
}

Upvotes: 2

Views: 9697

Answers (2)

Arun C
Arun C

Reputation: 9035

In order to dynamically generate View Id use form API 17

generateViewId()

Which will generate a value suitable for use in setId(int). This value will not collide with ID values generated at build time by aapt for R.id.

Like this

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EditText editText = new EditText(MainActivity.this);
                editText.setId(editText.generateViewId());
                editText.setHeight(50);
                editText.setWidth(50);
                ll.addView(editText);

            }

Upvotes: 5

Onur A.
Onur A.

Reputation: 3017

You can make a list of possible ids in your resources folder, like ids.xml and inside it place the ids like below;

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <item type="id" name="edittext1" />
        <item type="id" name="edittext2" />
        <item type="id" name="edittext3" />
</resources>

then in your Java code set dynamic ids to your EditTexts like this;

youreditText1.setId(R.id.edittext1);
youreditText2.setId(R.id.edittext2);
youreditText3.setId(R.id.edittext3);

Upvotes: 3

Related Questions