android - Set tab's title from fragment on the fly -
i have on activity (mainactivity) , has 5 tabs, each tab contains fragment. each fragment contains edittext
widget. i'd set active tab's title when change text of edittext in active fragment's class. how on fly ? here resources:
mainactivity:
public class mainactivity extends appcompatactivity { private sectionspageradapter msectionspageradapter; private viewpager mviewpager; imageview imageview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); mviewpager = (viewpager) findviewbyid(r.id.container); mviewpager.setadapter(msectionspageradapter); tablayout tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(mviewpager); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
sectionpageradapter:
public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { switch (position){ case 0: tab1 tab1 = new tab1(); return tab1; case 1: tab2 tab2 = new tab2(); return tab2; case 2: tab3 tab3 = new tab3(); return tab3; case 3: tab4 tab4 = new tab4(); return tab4; case 4: tab5 tab5 = new tab5(); return tab5; default: return null; } } @override public int getcount() { // show 5 total pages. return 5; } @override public charsequence getpagetitle(int position) { switch (position) { case 0: return "title1"; case 1: return "title2"; case 2: return "title3"; case 3: return "title4"; case 4: return "title5"; } return null; } }
tab1:
public class tab1 extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.tab1, container, false); return rootview; } }
these generated android studio, guess should go here. want, if have edittext
on tab1, , start type in it, changes first tab's title "title1" string typed.
from edittext should use addtextchangedlistener
on edittext
within textwatcher
instance change title on fly. refer code.
edittext.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) { //this tab index want change title of.. tablayout.gettabat(1).settext(charsequence); } @override public void aftertextchanged(editable editable) { } });
hope helps..
wiki
Comments
Post a Comment