I have a complex view, containing several subviews, like text views and image views.
I'd like to replace one of the image views with another (derived) image view (the other one support loading images in the background).

How can I replace the original image view with a new one?

The solution I currently have is just copy pasting the whole XML layout and do the replace in the new XML - Very bad to duplicate and not reuse things :(

Regrading the followup, include is good when you want to add an existing layout to a another one. Here, I want to replace a view within another - programmatically - not compile time.
– AlikElzin-kilakaJun 12 '13 at 11:15

1

Small addition: before adding newView to the parent in replaceView, it might be useful to copy the currentViews id: newView.setId(currentView.getId()); so that findViewById returns newView
– Frederic KleinSep 6 '16 at 19:48

Is it me or the index is for LinearLayout and you will need to do something different for RelativeLayout: newView.setLayoutParams(currentView.getLayoutParams());. Another thing that will be missed in regards to @FredericKlein id is the onClick.
– MikeLNov 1 '18 at 15:05