layout inflater - How to set and reuse source code on android -
i'm having trouble on developing android apps. want reuse section many other activities. , found "layout inflater". can use that.
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <!-- header start --> <relativelayout android:id="@+id/header" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:padding="5dp" android:background="#fff8f8ff"> <!-- menu button --> <button android:id="@+id/menubtn" android:layout_width="40dp" android:layout_height="30dp" android:layout_marginleft="10dp" android:background="@drawable/menubtn"/> <!-- logo / return main page --> <button android:id="@+id/logobtn" android:layout_width="30dp" android:layout_height="30dp" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:background="@drawable/ic_launcher"/> <!-- login btn --> <button android:id="@+id/loginbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@id/logobtn" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_marginright="10dp" android:background="#00ffffff" android:text="login" android:textsize="20dp" /> </relativelayout> <!-- end of header --> </relativelayout>
but, now! having problem next step. how set , reuse activity button listener , that.
// header button listener button menubtn = (button)findviewbyid(r.id.menubtn); button logobtn = (button)findviewbyid(r.id.logobtn); button loginbtn = (button)findviewbyid(r.id.loginbtn); menubtn.setonclicklistener(new onclicklistener(){ public void onclick(view v){ toast.maketext(getbasecontext(), "menu", toast.length_short).show(); } }); logobtn.setonclicklistener(new onclicklistener(){ public void onclick(view v){ toast.maketext(getbasecontext(), "logo", toast.length_short).show(); } }); loginbtn.setonclicklistener(new onclicklistener(){ public void onclick(view v){ toast.maketext(getbasecontext(), "login", toast.length_short).show(); } }); // end of header button
i want reuse section other activities. , trying import system but, not yet.
shortly, want reuse source code(listener , xml). how can that?
public class youractivity extends activity implements button.onclicklistener{ protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); menubtn.setonclicklistener(this); logobtn.setonclicklistener(this); loginbtn.setonclicklistener(this); } public void onclick(view v) { if(menubtn == v) { showmytoast("menu"); }else if(logobtn == v){ showmytoast("logo"); }else if(loginbtn == v){ showmytoast("login"); } } private void showmytoast(string str){ toast.maketext(this, str, toast.length_short).show(); } }
Comments
Post a Comment