0

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?

mkr231
  • 68
  • 2
  • 16

1 Answers1

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