0

I want to make a new layout file at run time (via source code) and save it into res/layout. I tried may ways for this but not successful.

One of the way I tried:

    getApplicationContext().getResources().getString(R.layout.activity_main);

    File src = new File(src);

    FileInputStream stream = null;

    try {
        stream = new FileInputStream(src);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

But I am getting FileNotFoundException.

Please help me .

  • I need to read activity_main and copy its content into some other file which i have to create now at run time and save it into res/layout. – anant mittal Oct 08 '15 at 08:36

2 Answers2

1

In Android resource files are pre compiled in APK and in class R the id references are stores for performance reasons. It doesn't make any sense you trying to do so, it is not possible. What is the purpose of this?

When you like to have dynamic layouts create them by code.

Thomas R.
  • 7,988
  • 3
  • 30
  • 39
  • I have a created a activity which have some buttons. Now I want to interchange the position of button while using the activity. I mean for example, lets say I have 3 buttons, Yes, No, Cancel. But if some one wants to have button YES at left most side. So he wants to swap it with other button placed at left most side. For this purpose I want to modify the layout file at run time. – anant mittal Oct 08 '15 at 08:40
  • I know it can be done without modifying the layout file too but I have some other 3rd party application dependency also which is fed layout file to work. So i need to update my layout file also in order to feed updated result to that 3rd party app which I am using along with my app. – anant mittal Oct 08 '15 at 08:41
  • Then create all layout files if applicable and when the user selects an options of layout you can recreate the current layout and use another layout reference. – Thomas R. Oct 08 '15 at 08:45
  • but the question is recreating this new layout. My original layout is very complex and i just need to swap two buttons. And I need to keep this change saved for next time use. And i need this change in form of updated layout file in order to make 3rd party app work correctly. Is it somehow possible to read any layout file and then create a new XML file with some changes and saving it into assets or some external sotrage(if not possible to put it into res folder as apk is already built) – anant mittal Oct 08 '15 at 08:51
  • Then add all parts, e.g. using includes for button layout, and then change visibility of layouts. – Thomas R. Oct 08 '15 at 08:52
  • Is it somehow possible to read any layout file and then create a new XML file with some changes and saving it into assets or some external sotrage(if not possible to put it into res folder as apk is already built) – anant mittal Oct 08 '15 at 08:53
  • Thanks for alternatives but please guide me for this particular scenario if its possible – anant mittal Oct 08 '15 at 08:54
  • I can't give you the right solution as I don't know your code. Instead of trying to create dynamic XML files you should dynamically create your layout/views by code. – Thomas R. Oct 08 '15 at 08:55
  • Yes Thomas you are correct. I must change my views dynamically rather than changing my XML file itself. But now I also think that its not possible to pick some XML file from some random phone memory and inflate it in activity. It would be so silly as R file can not be regenerated. – anant mittal Oct 08 '15 at 08:58
0

That's not possible.You can't modify resources once apk file is generated.

Jas
  • 3,207
  • 2
  • 15
  • 45
  • I am ok with saving my new file into assets or external storage of device but I am still stuck with reading file content present in res/layout and copying it into newly created file at run time and save it into assets or external storage. – anant mittal Oct 08 '15 at 08:46
  • Refer this http://stackoverflow.com/questions/4591696/android-how-to-generate-xml-resources-on-runtime – Jas Oct 08 '15 at 08:50
  • Thanks Jas. I think it might solve my problem. but i need to find out how assetmanager will work. – anant mittal Oct 08 '15 at 09:03