Android: connect view to class for later multiplying -


i have such small layout:

<linearlayout     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:gravity="center"     android:orientation="vertical" >      <checkbox         android:id="@+id/selectorcheckbox"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:contentdescription="@string/imagecountryflag" />      <imageview         android:id="@+id/selectorflag"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:contentdescription="@string/imagecountryflag"         android:src="@drawable/us" /> </linearlayout> 

i connect layout specific class handling changing of imageview , checking of checkbox depend on parameters in constructor.

 class mypanel  {      public mypanel(boolean ischecked, string flagpath)      {           changeflag(flagpath);           changecheckout(ischecked);      }       private void changeflag(string flagpath)      { /*change flag*/ }       private void changecheckout(boolean ischecked)      { /*change check out*/ }  } 

after duplicate mypanel , have couple of checkboxes different flags , change class mypanel. this: enter image description here

is possible , how this?

you can create custom view , inflate layout init example:

public class mypanel extends linearlayout {      private checkbox cb;     private imageview iv;      public mypanel(context context) {         this(context, null);     }       public mypanel(context context, attributeset attrs) {         super(context, attrs);         layoutinflater inflater = (layoutinflater) context                 .getsystemservice(context.layout_inflater_service);          inflater.inflate(r.layout.your_layout, this, true);          cb= (checkbox) findviewbyid(r.id.selectorcheckbox);             iv= (imageview)findviewbyid(r.id.selectorflag);      }    private void changeflag(string flagpath)  { /*change flag*/ }   private void changecheckout(boolean ischecked)  { /*change check out*/ } } 

you can use in xml:

<your_pkg_name.mypanel     android:id="@+id/my_panel"     android:layout_width="match_parent"     android:layout_height="wrap_content" > </your_pkg_name.mypanel> 

Comments