android - generate main.xml inside onCreate() -
is possible generate main.xml inside oncreate()? such as:
package avm.project; import java.io.bufferedwriter; import java.io.file; import java.io.filewriter; import java.lang.reflect.field; import android.app.activity; import android.content.res.resources; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.viewflipper; public class avmordersystemactivity extends activity { viewflipper flipper; @override public void oncreate(bundle icicle) { super.oncreate(icicle); //file[] listoffiles = folder.listfiles(); /*for (int = 0; < listoffiles.length; i++) { if (listoffiles[i].isfile()) { log.i("xml generator","file " + listoffiles[i].getname()); } else if (listoffiles[i].isdirectory()) { log.i("xml generator","directory " + listoffiles[i].getname()); } }*/ string xml=""; xml=xml+"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; xml=xml+"<relativelayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"+ "android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\"\n"+ "android:orientation=\"horizontal\" android:padding= \"5px\">\n"; xml=xml+"<viewflipper android:id=\"@+id/adflipper\"\n"+ "android:inanimation=\"@android:anim/fade_in\" android:outanimation=\"@android:anim/slide_out_right\"\n"+ "android:paddingleft=\"15px\" android:layout_width=\"fill_parent\"\n"+ "android:layout_height=\"fill_parent\" android:autostart=\"true\"\n"+ "android:flipinterval=\"5000\">"; resources a=this.getresources(); try { (int i=0x7f020000 ;i<0x7f020020;i++) { string name=a.getresourcename(i); xml=xml+"<imageview android:layout_width=\"fill_parent\" android:src=\"@"+name.substring(name.indexof(':')+1)+"\"\n"+ "android:layout_height=\"fill_parent\"></imageview>\n"; } } catch(exception e) { log.i("resoruces","exception"); e.printstacktrace(); } xml=xml+"</viewflipper>\n</relativelayout>"; xml=xml+"<!--xxx-->"; try{ // create file filewriter fstream = new filewriter("res/layout/main.xml"); bufferedwriter out = new bufferedwriter(fstream); out.write(xml); //close output stream out.close(); }catch (exception e){//catch exception if log.i("xml write","exception"); e.printstacktrace(); } setcontentview(r.layout.main); flipper=(viewflipper)findviewbyid(r.id.adflipper); button btn=(button)findviewbyid(r.id.restbutton); btn.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { flipper.shownext(); } }); } }
i have implemented code , can project drawables generate main.xml accordingly not working (i got exception: file not found res/layout/main.xml). (my aim generate main.xml according varying number of png images) doing wrong, possible on or there other solution?
thanks.
you can change src of imageview dynamically without having generate xml.
imageview imageview = (imageview) findviewbyid(r.id.your_image_view); imageview.setimagedrawable(getresources().getdrawable(r.drawable.your_png));
and can add more creating them dynamically , adding them parent relativelayout:
for (int i=0x7f020000 ;i<0x7f020020;i++) imageview imageview = new imageview(this); imageview.setbackgroundresource(r.drawable.your_png_reference); relativelayout ll = (relativelayout) findviewbyid(r.id.your_relative_layout_id); ll.addview(imageview); }
Comments
Post a Comment