Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load XML layout from server (android)

I had lots of research about this issue but I didn't get the answer that I want. So I do have an application getting string from the server. The series of string is in XML Format.

Here is an example of what I shall get from the server (as you can see it's a layout):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/number_ref" />
    <EditText 
        android:id="@+id/etTxtNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/subject_ref" />
    <EditText 
        android:id="@+id/etTxtSubject"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/body_ref" />
    <EditText 
        android:id="@+id/etTxtBody"
        android:layout_width="match_parent"
        android:layout_height="200dp" />
    <LinearLayout 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal" >
          <Button 
                android:id="@+id/btnTxtSave"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/save_ref"/>
          <Button 
                android:id="@+id/btnTxtSend"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/send_ref"/>
          <Button 
                android:id="@+id/btnTxtClose"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/close_ref"/>
    </LinearLayout>

</LinearLayout>

This will be changed depending on what xml file will be uploaded from the server. My problem is how can I implement these series of string into layout and to be loaded as layout of an activity. I was thinking of saving it in an xml file to sdcard of the device and load it as layout but I guess it's not possible to recompile the code after running it. Any suggestions? Thank you.

like image 288
Nina Avatar asked Mar 07 '14 01:03

Nina


1 Answers

As explained in the Android documentation, the layout xml files are not used at runtime - they are compiled to binary code at compile time. So, you can't directly load the xml as a Layout.

You'd have to write code that reads the xml, interprets it, and uses Android api methods to create a corresponding View.

like image 171
GreyBeardedGeek Avatar answered Sep 28 '22 17:09

GreyBeardedGeek