1

I'm working on a project for my university about customizing an Android application on runtime.

Is it possible to include layout XML files that are actually not in the \res\layout\ folder of my android project but on an external webserver? The idea behind this is to give me an opportunity to customize the xml-file on my webserver, e.g. adding new fields, without having to re-install/update the app.

I'll try to explain my idea with the following example:

Let's say I got an application to save and display addresses. All addresses are stored in a database. I got the fields 'name' and 'surname' in my database and displayed in the app when saving or displaying the addresses.

Now I would like to add a third field 'email' where I can enter email addresses.

My idea is to create this field in my database and add it to my layout-xml-file which is on my webserver. So I can 'link' the xml file within the app and the new field appears after refreshing the app.

Hope you guys can give me some information about how to customize my input forms on runtime, my research on the internet didnt help me out at all..

Greetz

1

2 Answers 2

1

You can't use external XML files to dynamically adjust the layout.

The documentation of LayoutInflater#inflate(XmlPullParser, ViewGroup) explains why:

Important For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

But you can create your layout dynamically in code, see e.g. Android Runtime Layout Tutorial

So you could put a file on the server that holds layout information, fetch it in your app, parse it and create the layout dynamically. It is going to be a lot of work since you would basically replicate Android's LayoutInflater. You can obviously simplify the format to just the basics that you need but it's still a lot of work that is IMHO not required.

Let's assume your layout info is just a plain text file that has name of fields

name
surname
email

You can then read it line by line and create a simple EditText for each in a way like

private View getDynamicLayout(ArrayList<String> lines) {
    LinearLayout ll = new LinearLayout(this);
    for (String line : lines) {
        EditText et = new EditText(this);
        // fill it with some text
        et.setText(line);
        ll.addView(ll);
    }
    return ll;
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ArrayList<String> layoutConfig = readConfigFromFile();
    View layout = getDynamicLayout(layoutConfig);
    setContentView(layout);
}

But unless you really need that, I would suggest that you update the app the regular way. It is also faster to use preprocessed layouts instead of building that info on your own.

Also take care not do heavy disk / any network access in the UI thread like in above example (reading the file). That should be done in a background task.

0
0

Thanks a lot for your help. I'll try to get through this.

The reason I can't just update the app is that the app I'm talking about is actually a CRM system used by many different companies. They all have to download the same app from the google play store.

The goal of my work is that every company can customize their input forms on their own, so there could be many different input forms (each for every company) but all using the same app.

I thought about a server that stores a file with the layout information which can be accessed by the app on runtime.

btw, since I'm not a software engineer, I don't have to write any code, I only have to describe the concept in an abstract way.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.