How to load XML layout from storage with layout.inflator?

129 views Asked by At

How can I load XML layout from storage by passing layout.inflator(XmlPullParser,null)?

My code is below:

import android.app.*;
import android.os.*;
import android.view.*;
import org.xmlpull.v1.*;
import android.net.*;
import android.content.*;
import java.io.*;
import android.widget.*;


public class inflater extends Activity
{
View v;
XmlPullParser p;
String uri;
String filelocat;
FileReader fr;
InputStream fi;

@Override
protected void onCreate(Bundle savedInstanceState)
{


    LinearLayout loi=findViewById(R.id.main2LinearLayout);
    super.onCreate(savedInstanceState);

    LayoutInflater li= LayoutInflater.from(this);

    try
    {
        p=ok("/storage/emulated/0/AppProjects/layoutt/Layout/app/src/main/res/layout/main.xml");
        Thread.sleep(4000);

    }
    catch (InterruptedException e)
    {}
    v=li.inflate(p,loi);
    Toast.makeText(this,p.toString(),Toast.LENGTH_LONG).show();
    setContentView(R.layout.main2);
}
private XmlPullParser ok(String uri){
    XmlPullParser  parser=null;

    try
    {

        fi = new FileInputStream(new File(uri));
    }
    catch (FileNotFoundException e)
    {}

    try
    {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();  
        factory.setNamespaceAware(true);  

        parser = factory.newPullParser();
        parser.setInput(fi, null);

    }
    catch (XmlPullParserException e)
    {}  
    return parser;
}
}

This line doing all the trouble:

v=li.inflate(p,loi);

I tried as well:

v=li.inflate(p,null);

None of them worked

As you can see I try to pass a layout XML file which is properly formatted. Also I gave all the permission. I start this activity from my launcher Activity by a button click. It doesn't work right

1

There are 1 answers

0
Siddharth Rout On

It only work on build time, 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.