duong_dajgja
duong_dajgja

Reputation: 4276

Change activity's layout by an XML string contained in main memory in runtime

I have an activity that is displaying to user. On the activity I have a button. Every time when user pushes the button, the app will load an xml file from a remote server. The xml file is actually a layout designed for the application. Right after the new xml file is loaded and stored in the newLayout variable, I would like to set the xml file to replace the current layout that is showing to user.

String newLayoutStr = "";
onClickButton() {
    newLayoutStr = loadNewLayoutFromRemoteServer();
}

// set the layout contained in newLayoutStr as new layout of the current activity

Any comments?

Upvotes: 1

Views: 326

Answers (3)

kandroidj
kandroidj

Reputation: 13922

This will not be possible with what your asking. Android relies on Resource identifiers using a class call R which is automatically generated. The file contains public static final int references for various class values in your application. The classes referenced here can be found at http://developer.android.com/reference/android/R.html

This class is than used to reference the various resources in your application, one of which is layout. So the system expects all layouts defined in xml to be included in this generated R class.

So in one sentence: you will not be able to accomplish this the way you want.

What i would do is have a json response and convert it to dynamically generated layout.

for XML you download test.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_height="match_parent"
      android:layout_width="match_parent">

      <TextView android:id="@+id/textViewTest"
           android:layout_centerInParent="true"
           android:gravity="center"
           android:layout_height="wrap_content"
           android:layout_width="match_parent"/>

  </RelativeLayout>

If you want to have this layout created in your app create a json response:

{
"layout_parent":"RelativeLayout",
"layout_height": "match_parent",
"layout_width": "match_parent",
"layout_children": [
        {
            "layout_child":"TextView",
            "layout_child_id":"textViewTest",
            "layout_height":"wrap_content",
            "layout_width":"match_parent",
            "layout_gravity":"center",
            "layout_centerInParent":"true"
        }
    ]
}

i would then parse this to a model object and use that model object to build your layouts dynamically. In order to add the fields though you will need to have some layout you defined in xml to be able to add more fields to in your app.

Upvotes: 2

nkigen
nkigen

Reputation: 450

As far as I know its currently not possible to load Layout dynamically at runtime that were not available at compile time. This is because the LayoutInflater needs a resource ID(from the R class) and this is determined at compile time. An alternative will be to implement a custom LayoutInflater(by borrowing some ideas from the LayoutInflater class) that can do this though this will be very slow as a lot of optimization is done at compile time

Upvotes: 0

Dnlhoust
Dnlhoust

Reputation: 118

I'm not sure this can be done at runtime from memory. You may be able to marshall the XML and save it to the SD card, then load it as a resource using the AssetManager

From there you can inflate the XML using getLayoutInflater().inflate(YourResourceID, TheRootView)

Upvotes: -1

Related Questions