0

I'm doing an Android project and I'm trying to figure out how to be dynamic in layouts.

So I have layout files complex_card.xml and simple_card.xml, each containing a ConstraintLayout tag with some other layout elements inside. complex_card takes more space than simple_card so I will use complex_card whenever I have a lot of space and can switch back to using simple_card when I don't have space.

In activity_main.xml, I've use both of the layouts via <include layout="R.layout.complex_card"/> tags. However, in real time, I want to be able to change that to <include layout="R.layout.simple_card"/>. How can I do that?

Lucas Cabrales
  • 2,073
  • 1
  • 12
  • 21
157 239n
  • 349
  • 3
  • 15
  • 1
    Possible duplicate of [How can I programmatically include layout in Android?](https://stackoverflow.com/questions/18999601/how-can-i-programmatically-include-layout-in-android) –  May 11 '18 at 20:23
  • No that won't be good because I want to be able to switch layouts from simple_card to complex_card multiple times but with ViewStub, you can inflate the layout only once. – 157 239n May 11 '18 at 22:50
  • Consider using [addView()](https://developer.android.com/reference/android/view/ViewGroup#addview) / [removeView()](https://developer.android.com/reference/android/view/ViewGroup#removeview) to replace your views. Maybe [ViewFlipper](https://developer.android.com/reference/android/widget/ViewFlipper) will suit better. – Rodrigo Queiroz May 11 '18 at 23:27

1 Answers1

0

I've found a work around to this. simple_card is a light weight version of complex_card which means I can use view.visibility = View.GONE and view.visibility = View.VISIBLE to hide certain layouts in complex_card which looks exactly like simple_card.

However, the original problem isn't solved if there are 2 completely different layout files.

157 239n
  • 349
  • 3
  • 15