0

I am writing an Android app that uses a standard layout fragment in different locations. The fragment is stored in an XML file, but I have one small problem with it: although the layout fragment is more or less standardized, I need to be able to modify certain aspects of it dynamically when loading it. Is there any way that I could programmatically retrieve an instance of the XML file, modify the aspects that need to be modified, and then add it to the activity's layout?

DaveTheMinion
  • 664
  • 3
  • 22
  • 45

1 Answers1

1

You can't modify xml layout files at runtime. The xml is compiled to Java code and can't be modified programatically.

What you can do though is creating views or changing positions of existing views programatically.

Here is an example on how to add a view.

Another one for changing positions.

Update:

You could even take it to another level by creating custom views.

Community
  • 1
  • 1
arsent
  • 6,975
  • 3
  • 32
  • 31
  • How could I go about doing that? I have researched views, but always avoided them because they look confusing. – DaveTheMinion Apr 05 '16 at 02:51
  • I updated the answer with 2 links to SO Q&As for both adding and modifying positions. It's a useful think to learn and it's not difficult once you get the idea. – arsent Apr 05 '16 at 02:52
  • Thank you. In case you are wondering, here is the issue that I am trying to resolve: http://stackoverflow.com/q/36393421/2941352 – DaveTheMinion Apr 05 '16 at 02:54
  • Thank you. I have to go now, so if you come up with any ideas, please let me know, and I will get back to it tomorrow. – DaveTheMinion Apr 05 '16 at 02:56