How to change the color of a selected tab in android tab host? -



How to change the color of a selected tab in android tab host? -

i have tab widget can alter kid tab color when selected. buggy, border lines android tab widget not consistently working well. borders lines turns color white when selecting tab create clear, code activity. im sorry if can't provide images because haven't earned lot of reputations here @ stack overflow

@suppresswarnings("deprecation") public class tabhostactivity extends tabactivity { private tabhost tabhost; private int currenttab = 0; private int lasttab = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_tabhost); useraccount useraccount = new useraccount(); tabhost = (tabhost) findviewbyid(android.r.id.tabhost); tabspec tab1 = tabhost.newtabspec("first tab"); tab1.setcontent(new intent(this, homeactivity.class)); tab1.setindicator("",getresources().getdrawable(r.drawable.home_icon)); tabhost.addtab(tab1); tabspec tab2 = tabhost.newtabspec("second tab"); tab2.setcontent(new intent(this, about.class)); tab2.setindicator("",getresources().getdrawable(r.drawable.about_icon)); tabhost.addtab(tab2); tabspec tab3 = tabhost.newtabspec("third tab"); tab3.setcontent(new intent(this, gridviewactivity.class)); tab3.setindicator("",getresources().getdrawable(r.drawable.gallery_icon)); tabhost.addtab(tab3); gettabhost().setontabchangedlistener(new ontabchangelistener() { public void ontabchanged(string tabid) { currenttab = gettabhost().getcurrenttab(); setcurrenttabcolor(); lasttab =currenttab; } }); gettabwidget().getchildat(lasttab).setbackgroundcolor(color.gray); } public void setcurrenttabcolor(){ gettabwidget().getchildat(currenttab).setbackgroundcolor(color.gray); gettabwidget().getchildat(lasttab).setbackgroundcolor(color.parsecolor("#fcafa6")); } }

this xml tabhost set background color of tab widget pink

<?xml version="1.0" encoding="utf-8"?> <tabhost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <linearlayout android:id="@+id/linearlayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <framelayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > </framelayout> <tabwidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" android:background="#fcafa6" > </tabwidget> </linearlayout> </tabhost>

first, in activity, instantiated tab host

tabhost = (fragmenttabhost) findviewbyid(r.id.tabcontainer); tabhost.setup(this, getsupportfragmentmanager(), r.id.tabcontent); tabhost.addtab(tabhost.newtabspec("1").setindicator("new tab"), pagefragment.class, null);

i wrote method update tabs's color based on whether or not selected

protected void updatetabs() { (int = 0; < tabhost.gettabwidget().getchildcount(); i++) { if (tabhost.gettabwidget().getchildat(i).isselected()) { sessionstate.getselectedindex().setselectedpage(i); tabhost.gettabwidget() .getchildat(i) .setbackgroundresource( r.drawable.tab_selected_background); } else { tabhost.gettabwidget() .getchildat(i) .setbackgroundresource( r.drawable.tab_unselected_background); } } }

then called method every time tab changed

tabhost.setontabchangedlistener(new ontabchangelistener() { @override public void ontabchanged(string tabid) { updatetabs(); } });

i hope helps

android tabs android-tabhost tabwidget

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -