Search code examples
androidandroid-layoutandroid-contactsviewgroup

How to add a layout with the click of button and id each element


Here is my image

Here is my image

enter image description here

How can i add a new layout PROGRAMATICALLY(with a button and an EditText)on CLICK of a '+' button and set a unique id for each child(like in the pic).


Solution

  • private Button buttonItemArray[] = new Button[50];
    private EditText edtValueArray[] = new EditText[50];
    
    
                int itemId = itemCounter;
                int valueId = valueCounter;
    
                LinearLayout linearLayout = new LinearLayout(
                        getApplicationContext());
                linearLayout.setOrientation(0);
    
                buttonItemArray[j] = new Button(NewVendorDetailActivity.this);
                buttonItemArray[j].setLayoutParams(edtName.getLayoutParams());
                buttonItemArray[j].setId(itemId);
                buttonItemArray[j].setText(name);
    
                        edtValueArray[j] = new EditText(NewVendorDetailActivity.this);
            edtValueArray[j].setLayoutParams(edtName.getLayoutParams());
            edtValueArray[j].setId(valueId);
               edtValueArray[j].setBackgroundResource(android.R.drawable.editbox_background_normal);
            edtValueArray[j].setSingleLine(true);
            edtValueArray[j].setText(value);
    
            linearLayout.addView(txtItemArray[j]);
            linearLayout.addView(edtValueArray[j]);
    
    
    
                layout.addView(linearLayout);
                itemCounter++;
                valueCounter++;
                j++;
    

    try this type add button custom ..........