java - Changing Tab Layout from Words to Icons -
i'm still ever learning code need one...
i wanted change words on tab view icons,
how that?
this main.java
public class main extends actionbaractivity { toolbar toolbar; viewpager pager; viewpageradapter adapter; slidingtablayout tabs; charsequence titles[] = {"home", "events", "bluetooth chat"}; int numboftabs = 3; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); getsupportactionbar().setelevation(0); // creating toolbar , setting toolbar activity. // creating viewpageradapter , passing fragment manager, titles fot tabs , number of tabs. adapter = new viewpageradapter(getsupportfragmentmanager(), titles, numboftabs); // assigning viewpager view , setting adapter. pager = (viewpager) findviewbyid(r.id.pager); pager.setadapter(adapter); // assiging sliding tab layout view. tabs = (slidingtablayout) findviewbyid(r.id.tabs); tabs.setdistributeevenly(true); // make tabs fixed set true, makes tabs space evenly in available width // setting custom color scroll bar indicator of tab view tabs.setcustomtabcolorizer(new slidingtablayout.tabcolorizer() { @override public int getindicatorcolor(int position) { return getresources().getcolor(r.color.tabsscrollcolor); } }); // setting viewpager slidingtabslayout. tabs.setviewpager(pager); final button b = new button(main.this); b.setbackgrounddrawable(getresources().getdrawable(r.drawable.button_pressed)); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } public void main2(view view) { startactivity(new intent(this, main2.class)); } public void bluetooth(view view) { startactivity(new intent(this, mainactivity.class)); } @override public void onbackpressed() { main.this.finish(); } }
this viewpageradapter.java
public class viewpageradapter extends fragmentstatepageradapter { charsequence titles[]; // store titles of tabs going passed when viewpageradapter created int numboftabs; // store number of tabs, passed when viewpageradapter created // build constructor , assign passed values appropriate values in class public viewpageradapter(fragmentmanager fm,charsequence mtitles[], int mnumboftabsumb) { super(fm); this.titles = mtitles; this.numboftabs = mnumboftabsumb; } //this method return fragment every position in view pager @override public fragment getitem(int i) { switch (i) { case 0: return new tab1(); case 1: return new tab2(); case 2: return new tab4(); } return null; } // method returns titles tabs in tab strip @override public charsequence getpagetitle(int position) { return titles[position]; } // method returns number of tabs tabs strip @override public int getcount() { return numboftabs; } }
help appreciated.
you can use imagespan & spannablestring show icon instead of text.
first create array of drawable source,
int tabicons[] = {r.drawable.ic_tab_home, r.drawable.ic_tab_job, r.drawable.ic_tab_map, r.drawable.ic_tab_comments };
then override getpagetitle() this,
@override public charsequence getpagetitle(int position) { drawable drawable = getresources().getdrawable(tabicons[position]); drawable.setbounds(0, 0, 56, 56); imagespan imagespan = new imagespan(drawable); spannablestring spannablestring = new spannablestring(" "); spannablestring.setspan(imagespan, 0, spannablestring.length(), spanned.span_exclusive_exclusive); return spannablestring; }
Comments
Post a Comment