How to create a duplicate copy of Clone?

In my Android application, I want to duplicate an ImageButtonalready created one ImageButton.

I want to create a new ImageButtonprogrammatically having the same widht, height, background, image src, fields, etc. an already created button in an XML file. In short, I want to create a duplicate ImageButton.

I tried this

ImageButton mImageButton = (ImageButton) findViewById(R.id.ib);
Imagebutton duplicate = mImageButton;

But this only applies to mImageButton. Thus, the change duplicatealso causes a change in mImageButton.

Please help me. Thank...

+4
source share
2 answers

You cannot clone views, the way to do this is to create your view each time.

XML .

:

private void addImageButton(ViewGroup viewGroup) {    
    View v = LayoutInflater.from(this).inflate(R.layout.ib, null);
    viewGroup.addView(v);
}

Programatically:

private void addImageButton(ViewGroup viewGroup) {    
    ImageButton imageButton = new ImageButton(context);
    viewGroup.addView(imageButton);
}
+6

, . :

java.lang.IllegalStateException: .

removeView() .

view.setId(int id);

0

All Articles