-2

In an Android application I'm building, I have a single activity which has multiple buttons and textviews. Based on user actions, I hide the elements and/or show them. This is intentional, cause I don't want the user to change activity.

For further clarity, the activity is a type controller. So when a user enables an option different buttons show up. This is why I want all of them in the same activity.

I am concerned about performance. Currently everything is in a Constraint Layout. Is there an effective way of having mutliple UI elements in the same activity, and having the ability to hide and show them at will? Should I look into Fragments?

Thank you in advance.

GeneralNfG
  • 11
  • 3
  • https://stackoverflow.com/questions/25822656/what-are-the-differences-between-activity-and-fragment – Jaimin Modi May 21 '18 at 13:25
  • @JaiminModi Thank you for the link, I will study more about Fragments – GeneralNfG May 21 '18 at 14:14
  • Reading your requirement, I don't think `Fragment` fits your need. `Fragment` intents to contain a meaning section of an activity, it has its own job and can interact with others, like LoginFragment, ProfileFragment... You only want to show/hide some views within the activity base on interaction. Wrap your views that need to show/hide inside `ViewFlipper` instead. it's small, easy to use with small case like this. – Tam Huynh May 21 '18 at 17:04
  • @GeneralNfG Fragments can be used with ViewPager, ViewFlipper, NavigationDrawer, and if there any special requirement to show and update specific part on single screen. I don't think that you should use Fragment for such thing you asked. – Jaimin Modi May 23 '18 at 04:23
  • I looked into your suggestions and posted my conclusion as an answer. Thank you all very much. – GeneralNfG May 23 '18 at 08:06

1 Answers1

0

As it turns out there isn't a clear answer.

There are a few solutions that may prove useful, I list them below for anyone who might stumble onto this:

  • Fragment is useful when you have multiple independent parts of your layout. You are limited in the communication between elements of different Fragments.

  • ViewStub is used when you have some elements which are rarely used in your layout. This makes the initial loading of the layout lighter by not inflating the elements in the ViewStub. You can then inflate the elements on demand.

  • ViewFlipper allows you to cycle through a list of elements (or display them selectively using setDisplayedChild). It's mostly used for single elements, so I'm not sure about its performance if you were to use it for showing and hiding nested layouts.

  • ConstraintLayout is probably the best solution for cases similar to mine, where you have a layout of buttons and text which are hidden and displayed in a complex manner.

GeneralNfG
  • 11
  • 3