android - Add a different color for each action bar tabs separately -
android - Add a different color for each action bar tabs separately -
i need add together different color every tabs.
for eg: below image
mainactivity.java:
// add together new tab actionbar.addtab(actionbar.newtab().settext("home") .settablistener(tablistener)); actionbar.addtab(actionbar.newtab().settext("news") .settablistener(tablistener)); actionbar.addtab(actionbar.newtab().settext("latest") .settablistener(tablistener));
home.java:
public class home extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater .inflate(r.layout.fragment_home, container, false); ((textview) v.findviewbyid(r.id.textview)).settext("home"); homecoming v; } }
styles.xml:
<resources> <style name="appbasetheme" parent="android:theme.light"> </style> <!-- application theme. --> <style name="apptheme" parent="appbasetheme"> </style> </resources>
right created 3 tabs.now need add together different color each tab separately.i need suggestion regarding this.thank you.
yes,finally done it.
mainactivity.java:
public class mainactivity extends fragmentactivity { static viewpager tab; tabspageradapter tabadapter; actionbar actionbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tabadapter = new tabspageradapter(getsupportfragmentmanager()); tab = (viewpager) findviewbyid(r.id.pager); tab.setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { actionbar = getactionbar(); actionbar.setselectednavigationitem(position); } }); tab.setadapter(tabadapter); actionbar = getactionbar(); // enable tabs on action bar actionbar.setnavigationmode(actionbar.navigation_mode_tabs); actionbar.tablistener tablistener = new actionbar.tablistener() { @override public void ontabreselected(android.app.actionbar.tab tab, fragmenttransaction ft) { // todo auto-generated method stub } @override public void ontabselected(actionbar.tab tab, fragmenttransaction ft) { tab.setcurrentitem(tab.getposition()); } @override public void ontabunselected(android.app.actionbar.tab tab, fragmenttransaction ft) { // todo auto-generated method stub } }; layoutinflater inflater = (layoutinflater) getsystemservice(layout_inflater_service); actionbar.tab tab = actionbar.newtab().settext("home") .settablistener(new tablistener(this, home.class.getname())); view tabview = inflater.inflate(r.layout.fragment_home, null); tabview.setbackgroundresource(r.drawable.gradient_shape); // set custom // color tab.setcustomview(tabview); actionbar.addtab(tab); tab = actionbar.newtab().settext("news") .settablistener(new tablistener(this, news.class.getname())); view tabview2 = inflater.inflate(r.layout.fragment_news, null); tabview2.setbackgroundresource(r.drawable.gradient_shape2); // set // custom // color tab.setcustomview(tabview2); actionbar.addtab(tab); tab = actionbar.newtab().settext("latest") .settablistener(new tablistener(this, latest.class.getname())); view tabview3 = inflater.inflate(r.layout.fragment_latest, null); tabview3.setbackgroundresource(r.drawable.gradient_shape3); // set // custom // color tab.setcustomview(tabview3); actionbar.addtab(tab); } public static class tablistener extends fragment implements actionbar.tablistener { public tablistener(mainactivity mainactivity, string name) { // this(mainactivity,name); } @override public void ontabselected(android.app.actionbar.tab tab, fragmenttransaction ft) { tab.setcurrentitem(tab.getposition()); } @override public void ontabunselected(android.app.actionbar.tab tab, fragmenttransaction ft) { } @override public void ontabreselected(android.app.actionbar.tab tab, fragmenttransaction ft) { } } }
gradient_shape.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="4dp" /> <stroke android:width="1dp" android:color="#0078a5" /> <gradient android:angle="90" android:endcolor="#00adee" android:startcolor="#0078a5" /> <padding android:bottom="25dp" android:left="50dp" android:right="50dp" android:top="25dp" /> </shape>
output:
hope helpful.
android tabs android-actionbar
Comments
Post a Comment