android - Dynamically add multiple views and classes to the current view -


i interested in populating screen/activity user defined number of same views. each view have exact same layout: couple textviews , few buttons. thing each button control each textview display.

the way thinking implement have 1 xml , 1 java class. dependimg on number user inputs, populate screen many same views (using loop). question is, can done? how? thinking in right way?

please input or thoughts, code examples great too.

of course can done.

i think easiest situation, plus can extend, create helper functions take care of:

1) creating empty screen 2) create button screen 3) create textview screen , 4) create screen , populate it

you have decide proper root element views, depending on child arragement need. simplicity let's choose linearlayout, relativelayout or tablelayout example same, changes when add elements, have use additional parameters place them.

note function create empty custom view returns viewgroup ("where layouts derive from"). way, work viewgroups , define screen layout type once, inside createcustomview. can change type of screens there, , rest of code work ...

here code inspiration:

private viewgroup createcustomview(context context) {      linearlayout mycoolnewview=new linearlayout(context); // or relativelayout, etc..     return mycoolnewview; }   private button createbutton(context context, string buttontext) {     button newbutton=new button(context);     newbutton.settext(buttontext);     return newbutton; }  private textview createtext(context context, string initialtext) {     textview newtext=new textview(context);     newtext.settext(buttontext);     return newtext; }  private viewgroup createscreen(context context, int numberofbuttons, int numberoftextfields) {      viewgroup newscreen=createcustomview(context);     textview[] textviews=new textview[numberoftextfields];      (int i=0; i<numberoftextfields; i++) {           textviews[i]=createtext(context, "hi text "+i);            newscreen.addview(textviews[i]); // ideally provide here layoutparams place buttons      }     (int j=0; i<numberofbuttons; j++) {           button button=createbutton(context, "hi button "+j);           button.setonclicklistener(new onclicklistener() {               public void onclick (view clickedview) {                     // here have button keypress , know textviews                     textview[i%j].settext("hey pressed me");               }           });           newscreen.addview(button);     }     return newscreen; } 

so can:

viewgroup screen1=createscreen(context, 10, 10); viewgroup screen2=createscreen(context, 5, 3); viewgroup screen3=createscreen(context, 2, 5); 

and add screens parent layout, viewflipper, viewswitcher, etc... this:

 viewgroup parentlayoutofallscreens=findviewbyid(r.id.root_of_screens);  parentlayoutofallscreens.addview(screen1);  parentlayoutofallscreens.addview(screen2);  parentlayoutofallscreens.addview(screen3); 

in xml have create root layout, , name root_of_screens...

good coding !!! suppose there'll errors in code above, typed here, hope idea , tweak suit needs!

edit : v2.0 : extending view create new .java named "mycoolscreen.java" or whatever name, in same folder activity (for simplicity):

package ........ public class mycoolscreen extends linearlayout {      /** every view holds own buttons, , private, it's encapsulating */     private textview[] mtextviews; // <-- convention, members should start "m"     private button[] mbuttons;     private userpressedbuttons muserpressedbuttonslistener; // see below      /** following constructors must present custom view, , must call super */     public mycoolscreen(context context) {         // constructor use when creating view programmatically         super(context);     }      public mycoolscreen(context context, attributeset attrs) {          // constructor android calls when include custom view in xml         // can too!!          // attrs include numberofbuttons , numberoftextfields xml         // beyond example, read it, it's interesting          super(context, attrs); // must here custom views, or not work.                                // tells parent view continue construction.     }      public mycoolscreen(context context, attributeset attrs, int defstyle) {         // constructor android calls xml         super(context, attrs, defstyle);      }       /** create "init" method initialize view outside */     public void init(int numberoftextviews, int numberofbuttons) {         createscreen(numberoftextviews, numberofbuttons);     }       /** same */     private button createbutton(context context, string buttontext) {         button newbutton=new button(context);         newbutton.settext(buttontext);         return newbutton;     }      /** same */     private textview createtext(context context, string initialtext) {         textview newtext=new textview(context);         newtext.settext(buttontext);         return newtext;     }      /** tweak function doesnt return view, rather fills 1 :) */      private void createscreen(int numberofbuttons, int numberoftextfields) {          viewgroup newscreen=this; // it's view 1 gonna fill up!         mtextviews=new textview[numberoftextfields];         mbuttons=new button[numberofbuttons];         context context=getcontext(); // views know context after constructed          (int i=0; i<numberoftextfields; i++) {               mtextviews[i]=createtext(context, "hi text "+i);               newscreen.addview(textviews[i]); // ideally provide here layoutparams place buttons         }          (int j=0; i<numberofbuttons; j++) {               button button=createbutton(context, "hi button "+j);               button.setid(j);               button.setonclicklistener(new onclicklistener() {                   public void onclick (view clickedview) {                         // here have button keypress , know textviews                         if (muserpressedbuttonslistener!=null) muserpressedbuttonslistener.onbuttonpressed(j);                         textview[i%j].settext("hey pressed me");                   }               });               mbuttons[j]=button;               newscreen.addview(button);         }     }      public interface userpressedbuttons {         public void onbuttonpressed(int buttonnumber);     }      public void setuserpressedbuttonslistener (userpressedbuttons listener) {          muserpressedbuttonslistener=listener;     } } 

ok, use this, in activity can do:

    import ....... .mycoolscreen;     import ....... .mycoolscreen.userpressedbuttons;      .     .     .      mycoolscreen screen1=new mycoolscreen(context);     screen1.init(5,5); // initializes screen.      myrootlayout.addview(screen1); 

what's cool this, functionality totally encapsulated in custom view. , resides in .java, activity code clean, , can expand view functionality without making ugly.

it's common practice create interfaces , listeners views communicate outside world, example, can do:

     screen1.setuserpressedbuttonslistener(new mycoolscreen.userpressedbuttons() {          @override          public void onbuttonpressed (int number) {               // know user pressed button "number", , can stuff without               // having include inside mycoolscreen class. of course in example               // don't need @ moment, because view modify textfield, suppose               // 1 of buttons "rocket launch" , handle @ activity level, ie.                if (number==rocket_launch) rocketlauncher.settarget(10,10).launch(); // mycoolscreen doesnt know how launch rockets, activity maybe yes...           }      }); 

you can kinds of cool things new custom view. example, define:

     @override      public void ondraw(canvas c) {           c.drawellipse ...           c.drawrectangle ....      } 

and can paint circles, lines, etc... on textfields & buttons :) work, have put

    setwillnotdraw(false) on constructor. 

there might errors, typed code here, hope helps you!


Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -