A.B.
A.B.

Reputation: 103

Setting View from xml file at runtime in Android

I'm trying to make simple program that allows to "load" an .xml file and display it as the View in setContentView of a new activity. Basically, what I would like is that the view of the new activity would be the same as if I called

setContentView(R.layout.my_view)

where R.layout.my_view would be that xml file. But in my case, that file doesn't yet exist at compilation time, and should be loaded dynamically (e.g., from storage).

I guess I could parse my .xml file and build the view dynamically by code, but that feels like reinventing the wheel and it's also most likely that the views won't be identical. I thought there should be some easy way to do this, but all the methods I found so far from similar questions (e.g.,

getResources().getXml(R.layout.my_view); 
//or 
LayoutInflater.from(context).inflate(R.layout.my_view, null) )

seem to require the file already in compilation time. Is there something I'm missing?

Upvotes: 0

Views: 959

Answers (2)

snachmsm
snachmsm

Reputation: 19223

well, its not possible to load XML from server and parse it like usual layout XML. from LatourInflater doc:

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.

and it isn't related to generating IDs or similar mechanism, just performance - so consider that you really need this feature, it is kind of blocked for a reason ;)

but if you still really want to load layout from third party you can use json2view lib. good luck!

Upvotes: 1

Md. Enamul Haque
Md. Enamul Haque

Reputation: 1026

It is impossible as you describe. But you can do it by Java code.

When we create a layout xml file under layout resource folder and give some id using android:id="@+id/textview" IDE generate an ID R.java file that hold all IDs we assign.

When we call that view from Activity or Fragment we create an Object using that ID like

TextView textview = (TextView) findViewById(R.id.textview);

and user that.

If you want to load xml code form storage or server as a plan text in xml format. You will assign some ID for your views. But IDE won't generate any ID for that. So you can not create object of them.

So solution is you have to generate view by Java code that means programmatically.

    setContentView(R.layout.activity_main);

    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    width = width - (100/width)*80;

    LinearLayout layout = (LinearLayout) findViewById(R.id.lo_dynamic_view_container);
    LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

    TextView tv = new TextView(this);
    tv.setLayoutParams(lparams);
    tv.setWidth(width);
    float redious [] = { 0, 0, 8.3f, 8.5f, 8.2f, 8.9f, 0, 0 };
    ShapeDrawable shape = new ShapeDrawable (new RoundRectShape(redious,null,null));
    shape.getPaint().setColor(Color.GREEN);
    tv.setBackground(shape);
    layout.addView(tv);

Here activity_main.xml will have only one empty LinearLayout layout. You can generate view according to your condition or instruction document form storage or server and add them to LinearLayout.

Upvotes: 1

Related Questions