Reputation: 1739
I have designed an app in that I am using TabBar
and I want to change the BackgroundColor of TabBar
when selected or pressed.. In the class where I am implementing TabBar
is extending Fragment
not TabActivity
. I have implemented following
tabHost = (TabHost) v.findViewById(R.id.tabhost);
mLocalActivityManager = new LocalActivityManager(getActivity(), false);
Intent intentongoing = new Intent().setClass(getActivity(), Properties_org_screen.class);
TabSpec tabSpecOngoing = tabHost.newTabSpec("On Going").setIndicator("On Going").setContent(intentongoing);
Intent intentcomplete = new Intent().setClass(getActivity(), Properties_org_screen.class);
TabSpec tabSpecComplete = tabHost.newTabSpec("Completed").setIndicator("Completed").setContent(intentcomplete);
Intent intentproposed = new Intent().setClass(getActivity(), Properties_org_screen.class);
TabSpec tabSpecProposed = tabHost.newTabSpec("Proposed").setIndicator("Proposed").setContent(intentproposed);
mLocalActivityManager.dispatchCreate(savedInstanceState);
tabHost.setup(mLocalActivityManager);
tabHost.addTab(tabSpecOngoing);
tabHost.addTab(tabSpecComplete);
tabHost.addTab(tabSpecProposed);
tabHost.setCurrentTab(0);
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++)
{
tabHost.getTabWidget().getChildAt(i).setPadding(2,5,0,10);
Log.v("","In for loop");
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.WHITE);
}
**OnCreate Ends**
@Override
public void onTabChanged(String arg0)
{
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.blue_title));
}
}
I also tried to implement Custom_tab.xml
as,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/blue"/>
<item android:state_focused="true" android:drawable="@drawable/new_background"/>
<item android:state_pressed="true" android:drawable="@drawable/blue"/>
</selector>
and calling this file in
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="40dp"
android:layout_gravity="center" android:gravity="center" android:background="@drawable/custom_tab"/>
Still not working.. What I am missing??
Upvotes: 0
Views: 510
Reputation: 1739
Use this in OnCreate
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++)
{
tabHost.getTabWidget().getChildAt(i).setPadding(2,5,0,10);
Log.v("","In for loop");
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.WHITE);
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.new_background);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.blue_title));
and TabChanged
method
@Override
public void onTabChanged(String arg0) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.new_background);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.blue_title));
}
It will display the blue color to tab which is selected and a white image named new_background to the tab which is not selected..
and don't forget to write
implements OnTabChangeListener
after your class name
and
tabHost.setOnTabChangedListener(this);
after
tabHost = (TabHost) v.findViewById(R.id.tabhost);
This one is the Perfect Solution I guess.
Upvotes: 1