0

I would like to create an user interface that always displays a menu panel on the left side, and a content block which changes depending on which menu entry is selected, i.e something similar to many websites.

I am new to creating Android user interfaces so I am wondering how to properly and dynamically change the content.

Is it better to create two different views, each of them including the same panel fragment (meaning 2 instances ?) and a different content fragment, and change that view using Activity#setContentView(), or should I create a single view and replace the fragment of the content dynamically ?

Virus721
  • 8,061
  • 12
  • 67
  • 123

2 Answers2

1

You should definitely use the Fragment approach.

Use a FragmentActivity and use the FragmentManager (getFragmentManager in the Activity) to create a transition and replace the Fragment depending on what item the user clicked.

See: http://developer.android.com/training/basics/fragments/fragment-ui.html

Patrick Dorn
  • 756
  • 8
  • 13
  • Thanks, but what are the reasons to choose the fragment approach over the multiple views one ? – Virus721 Mar 04 '15 at 16:07
  • one reasons would be that you habe two different fragments with your UI logic. the view approach would suggest that you have all your logic in the activity what could be quite messy if your number of different views increase. – Patrick Dorn Mar 04 '15 at 16:12
  • Ok thanks. I found a SO question explaining how to replace a fragment : http://stackoverflow.com/questions/5658675/replacing-a-fragment-with-another-fragment-inside-activity-group But I wonder something : if I replace what is contained inside a view of given ID by a fragment, it's gonna remove all the children of that view right ? How could I replace only the N-th fragment without removing other present fragments, if any ? – Virus721 Mar 04 '15 at 16:22
0

Use this below code with library from github it will answar you perfectly https://github.com/neokree/MaterialNavigationDrawer

Shadik Khan
  • 1,217
  • 1
  • 8
  • 18