Custom title for android fragment using viewpager -
i want add custom title each fragment, in viewpager
. how can implement. in below code, need add custom title fragmentmain
, fragmentmore
, fragmentchatview
.
thanks in advance.
public class homeactivity extends fragmentactivity { // page adapter between fragment list , view pager public static pageradapter mpageradapter; // view pager public viewpager mpager; // activity data public string p2text, p3text; public static list<fragment> fragments;// = buildfragments(); // / arraylist<string> categories = {"1","2","3","4","5","6","7","8"}; arraylist<string> categories = new arraylist<string>(); static final string log_tag = "homeactivity"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); fragments = new arraylist<android.support.v4.app.fragment>(); categories.add("1"); categories.add("2"); categories.add("3"); categories.add("4"); categories.add("5"); categories.add("6"); categories.add("7"); addfragments(fragmentmore.class.getname(), 1); addfragments(fragmentmain.class.getname(), 2); addfragments(fragmentchatview.class.getname(), 3); mpager = (viewpager) super.findviewbyid(r.id.pager); mpageradapter = new pageradapter(this, getsupportfragmentmanager(), fragments, categories); mpager.setadapter(mpageradapter); mpager.setcurrentitem(1); } public void addfragments(string classname, int position) { // list<android.support.v4.app.fragment> fragments = new // arraylist<android.support.v4.app.fragment>(); // (int = 0; i<categories.size(); i++) { bundle b = new bundle(); b.putint("position", position); fragments.add(fragment.instantiate(this, classname, b)); // } // return fragments; } public void removefragments(string classname, int position) { // list<android.support.v4.app.fragment> fragments = new // arraylist<android.support.v4.app.fragment>(); // (int = 0; i<categories.size(); i++) { bundle b = new bundle(); b.putint("position", position); fragments.remove(fragment.instantiate(this, classname, b)); // } // return fragments; } private list<android.support.v4.app.fragment> buildfragments() { list<android.support.v4.app.fragment> fragments = new arraylist<android.support.v4.app.fragment>(); (int = 0; < categories.size(); i++) { bundle b = new bundle(); b.putint("position", i); fragments.add(fragment.instantiate(this, fragmentsearch.class.getname(), b)); } return fragments; } @override public void onresume() { super.onresume(); log.e(log_tag, "onresume"); } @override public void onpause() { super.onpause(); log.e(log_tag, "onpause"); } } public class fragmentmore extends fragment { button btnwrite; public string ptext="more"; static final string log_tag = "fragment more"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public void onactivitycreated (bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); } // oncreateview : @override public view oncreateview(layoutinflater inflater,viewgroup container,bundle savedinstancestate) { if (container == null) { return null; } // inflate view layout view view = (linearlayout)inflater.inflate(r.layout.more,container,false); return view; } @override public void onresume() { super.onresume(); log.e(log_tag, "onresume"); } @override public void onpause() { super.onpause(); log.e(log_tag, "onpause"); } }
//more layout
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/screen_back_color" android:gravity="center_horizontal|center_vertical" android:orientation="vertical" > <button android:id="@+id/btnsettings" android:layout_width="@dimen/more_button_width" android:layout_height="wrap_content" android:layout_marginbottom="@dimen/more_button_margin" android:layout_margintop="@dimen/more_button_margin" android:background="@drawable/button_selector_grey" android:text="@string/settings" android:textallcaps="true" android:textsize="@dimen/more_button_font_size" android:textcolor="@color/white" > </button> <button android:id="@+id/btntermsandconditions" android:layout_width="@dimen/more_button_width" android:layout_height="wrap_content" android:layout_marginbottom="@dimen/more_button_margin" android:layout_margintop="@dimen/more_button_margin" android:background="@drawable/button_selector_grey" android:text="@string/termsandcondition" android:textallcaps="true" android:textcolor="@color/white" > </button> </linearlayout>
//custom title layout
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_gravity="fill_horizontal" android:background="@color/black" android:orientation="horizontal" > <textview android:id="@+id/header_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:textsize="@dimen/main_edittext_fontsize" android:contentdescription="@string/desc" android:gravity="center" android:textcolor="@color/light_blue" android:paddingbottom="7dp" android:paddingtop="7dp" android:text="title" /> </relativelayout>
i guess looking pagertitlestrip element.
check out section of android tutorial http://developer.android.com/training/implementing-navigation/lateral.html#pagertitlestrip
Comments
Post a Comment