分享

Activities loading xml layout dynamically in android

 quasiceo 2015-07-01

Is it possible to load an activity's xml layout file from a resource stored in the device (in a db or part of the resources) and load it dynamically when that activity is started ? The idea is to send it to the device from a web service. Thanks.

asked Mar 16 '11 at 12:07
xain
1,96574892

5 Answers

If you are trying to inflate a XML file that was not included during the build process I don't think it is currently possible. This is from the java-docs of the LayoutInflater class:

View android.view.LayoutInflater.inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)

Inflate a new view hierarchy from the specified XML node. Throws InflateException if there is an error.

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.

answered Jan 16 '12 at 0:06
theJosh
1,402830

1  
If anyone is interested in trying this: there is an open source app AnDroidDraw that has a simple class for starting with. reference: code.google.com/p/droiddraw/source/browse/trunk/AnDroidDraw/src/… - I can post a slightly improved one that parses some colors, ImageView placeholders. I'd like to see if someone wants to expand on this. –  RoundSparrow hilltx Jul 12 '14 at 18:50

As of the date of this posting, Android only contains a built-in way to inflate layout XML stored as a layout resource in the APK file. If you want to inflate similar (or different) XML from other sources, you will have to implement that yourself, perhaps by cloning some logic from the LayoutInflater class. Be warned that Android does a lot of work to optimize reading and parsing of resource files at run-time, so if you plan on loading this dynamically on your own, be prepared for it to inflate a LOT slower.

answered Jan 17 '12 at 5:44
Alex Lockwood
42.1k19118178

If you have to load info from a db, maybe it helps you to do a basic XML, and insert info on it, like a table or something like that. You can try something like this:

In a basic table XML:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas./apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="@+id/SensorInfoTableLayout">

<TableRow>
    <TextView
            android:text="@string/sensor_name_title"
            android:padding="3dip" /> 
    <TextView
            android:text="@string/sensor_type_title"
            android:padding="3dip" />
    <TextView
            android:text="@string/sensor_value_title"
            android:padding="3dip" />
    <TextView
            android:text="@string/sensor_unit_title"
            android:padding="3dip" />
</TableRow>

<View
    android:layout_height="4dip"
    android:background="#FF909090" /></TableLayout>

And the code:

    @Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState); 

    getSensorInfo();
    setContentView(R.layout.sensorinfo);

    setInfoByView();
}

    private void setInfoByView()
{
    TableLayout myTableLayout = null;
    myTableLayout = (TableLayout)findViewById(R.id.SensorInfoTableLayout);

    // Add row to table
    myTableLayout.addView(createRow("maxRange",maxRangeType, "" + maxRange ,maxRangeUnits));
    myTableLayout.addView(createRow("minDelay",minDelayType,"" + minDelay, minDelayUnits));
    myTableLayout.addView(createRow("name",nameType,"" + name, nameUnits));
    myTableLayout.addView(createRow("powerReq",powerReqType,"" + powerReq, powerReqUnits));
    myTableLayout.addView(createRow("resolution",resolutionType,"" + resolution, resolutionUnits));
    myTableLayout.addView(createRow("type",typeType,"" + type, typeUnits));
    myTableLayout.addView(createRow("vendor",vendorType,"" + vendor, vendorUnits));
    myTableLayout.addView(createRow("version",versionType,"" + version, versionUnits));
}

    private TableRow createRow(String name, String type, String value, String unit)
{
    // Create new row
    TableRow myTableRow = new TableRow(this);
    myTableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    //myTableRow.setGravity(Gravity.CENTER_HORIZONTAL);
    myTableRow.setPadding(5, 5, 5, 5);
    // Add name
    myTableRow.addView(createTextView(name));
    // Add type
    myTableRow.addView(createTextView(type));
    // Add value
    myTableRow.addView(createTextView(value));
    // Add units
    myTableRow.addView(createTextView(unit));

    return myTableRow;
}

In the XML, it only exists the principal bar, with name, title, value and unit. And dinamically, add rows with info and style.

Maybe it helps, it works for me.

answered Jan 17 '12 at 9:41
Piperoman
4,98563379

Is it possible to load an activity's xml layout file from a resource stored in the device

Yes.

View inflatedView = View.inflate(this, R.layout.sample, null);
container.addView(inflatedView);

As @Chirag Raval said.

Is it possible to load an `activity's xml` layout file from a `db / web service`.

Don't think so. Because the layout reference (id) that you call from R.layout.XPTO is pre-compiled. You can't add values during runtime.


An middle-term option that you can use (and that I use in some apps) is set a pre-determined number o "blocks" that can be re-arranged according to the data that is sent. Example:

Receive text, image, text + image

Load: text_block.xml then image_block.xml and then text_and_image_block.xml

answered Jan 12 '12 at 16:25
NeTeInStEiN
8,83045887

    
I think you ommitted some words in the second question....Is it possible to load an activity's xml layout file from a db / web service. –  sonu thomas Jan 12 '12 at 17:31
    
Either way, the answer is clear. –  NeTeInStEiN Jan 12 '12 at 17:43

Yes,it is possible

Use LayoutInflater class

LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.dblayout, null);
setContentView(view);

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多