I have a relative layout containing an imageview and a textview under it in my layout folder. Is there any way to dynamically add a relative layout of the exact same properties in my current activity on the click of a button?
Dynamically adding a relative layout into an activity inheriting it's property from a generic layout
Asked
Active
Viewed 533 times
1 Answers
1
It would go something like this:
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ViewGroup container = (ViewGroup) v.getParent();
LayoutInflater inflater = getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View addView = inflater.inflate(R.layout.your_id);
container.addView(addView);
}
});
However this might not work the way you intended. To make sure the View is added at the position you want, create an empty Layout at that position, then use container
to reference that Layout and add the addView
to that Layout. Good luck!

nstosic
- 2,584
- 1
- 17
- 21