Class for reusable layout android -


i have reusable layout called title

<?xml version="1.0" encoding="utf-8"?> 

<textview     android:id="@+id/title_text"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentleft="true"     android:layout_alignparentright="true"     android:layout_centervertical="true"     android:gravity="center"     android:text="@string/lesson_title"     android:textsize="@dimen/title" />  <button     android:id="@+id/title_book"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentbottom="true"     android:layout_alignparentright="true"     android:layout_alignparenttop="true"     android:onclick="onclick"     android:text="@string/book" />  <button     android:id="@+id/title_buy"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentbottom="true"     android:layout_alignparentright="true"     android:layout_alignparenttop="true"     android:onclick="onclick"     android:text="@string/buy"     android:visibility="gone" /> 

i include in of activities , works fine.

to handle clicks , title text have in every activity

textview title = (textview) findviewbyid(r.id.title_text); title.settext(r.string.some_title); button book = (button) findviewbyid(r.id.title_book); button buy = (button) findviewbyid(r.id.title_buy); 

and clicks

public void onclick(view v) {      switch (v.getid()) {      case r.id.title_book:             toast.maketext(getbasecontext(), "book", toast.length_short).show();         break;      case r.id.title_buy:             toast.maketext(getbasecontext(), "buy", toast.length_short).show();         break;     } } 

is there way can create class handle this? have init method call activities set text on title because buttons same function everytime.

this have tried based on answer udi

public class customtitle extends relativelayout{  private context mcontext; private layoutinflater layoutinflater; private viewholder myviewholder; string mtext = "hello";  public customtitle(context context) {     super(context);     this.mcontext = context;     //this.mtext = text;      inflate();     bindviews(); }  private void inflate() {      if(layoutinflater == null){         layoutinflater = (layoutinflater) mcontext         .getsystemservice(context.layout_inflater_service);     }      layoutinflater.inflate(com.callan.app.r.layout.title, this, true);       }  private void bindviews() {     // bind views here      if(myviewholder == null){         myviewholder = new viewholder();     }      myviewholder.title_text = (textview) findviewbyid(com.callan.app.r.id.title_text);     myviewholder.book = (button) findviewbyid(com.callan.app.r.id.title_book);     myviewholder.buy = (button) findviewbyid(com.callan.app.r.id.title_buy);      if(callan.getmycredit() > 0){          myviewholder.buy.setvisibility(view.gone);         myviewholder.book.setvisibility(view.visible);     }else{          myviewholder.buy.setvisibility(view.visible);         myviewholder.book.setvisibility(view.gone);     }      myviewholder.title_text.settext(mtext);      }  class viewholder{      textview title_text;     button book;     button buy; }  public void onclick(view v) {      switch (v.getid()) {      case com.callan.app.r.id.title_book:             toast.maketext(mcontext, "book", toast.length_short).show();         break;      case com.callan.app.r.id.title_buy:             toast.maketext(mcontext, "buy", toast.length_short).show();         break;     } } 

}

and in xml have

<com.callan.custom_views.customtitle xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical" >  //my views 

then in activity have after oncreate

customtitle ctitle = new customtitle(this); 

exception

04-30 14:24:44.440: e/androidruntime(7184): java.lang.runtimeexception: unable start activity componentinfo{com.callm.app/com.callan.app.lessons}:       android.view.inflateexception: binary xml file line #2: error inflating class        com.callm.custom_views.customtitle  04-30 14:24:44.440: e/androidruntime(7184): caused by: android.view.inflateexception:       binary xml file line #2: error inflating class com.callm.custom_views.customtitle 

you can create custom class extends framelayout/or else want. in it's constructor create 2 methods: inflate() , bindviews()

public class customview extends framelayout {  private context mcontext; private layoutinflater layoutinflater; // views here  public customview (context context) {     super(context);     this.mcontext = context;     inflate();     bindviews();  }  private void inflate() {     layoutinflater = (layoutinflater) mcontext             .getsystemservice(context.layout_inflater_service);     layoutinflater.inflate(r.layout.your_xml_layout, this, true); }  private void bindviews() {     // bind views here } } 

add methods , you'll able use class wherever need.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -