Reputation: 165
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, NewUserActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
spec = tabHost.newTabSpec("New User").setIndicator("New User").setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ExistingUserActivity.class);
spec = tabHost.newTabSpec("Existing User").setIndicator("Existing User").setContent(intent);
tabHost.addTab(spec);
tabHost.getTabWidget()
.getChildAt(0)
.setLayoutParams(
new LinearLayout.LayoutParams((width / 2) - 2, 40));
tabHost.getTabWidget()
.getChildAt(1)
.setLayoutParams(
new LinearLayout.LayoutParams((width / 2) - 2, 40));
TabWidget tw = getTabWidget();
tw.getChildAt(0).setBackgroundColor(Color.parseColor("#800000"));
tw.getChildAt(1).setBackgroundColor(Color.parseColor("#FF6347"));
}
@Override
public void onTabChanged(String x) {
}
The above code initializes the TabHost . It uses two tabs. One- New User and another-Existing User. On clicking any of them, i want the tab to be focussed and of a different color than the other one. I have no idea how to use the OnTabChanged method.
Upvotes: 2
Views: 9609
Reputation: 22064
To setup tabs with different states, create these two methods:
private void setupTab(final View view, final String tag, Intent intent) {
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec tabSpec = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tabHost.addTab(tabSpec);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
tv.setTypeface(tf);
return view;
}
Then create tabs_bg.xml in layout folder:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Title"
android:textSize="15dip" android:textColor="@drawable/tab_text_selector" />
</LinearLayout>
Now in drawable folder, create the xml files. tab_bg_selector.xml, tab_bg_selected.xml and tab_bg_unselected.xml:
--- TAB SELECTOR ---
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
<!-- Inactive tab -->
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
<!-- Pressed tab -->
<item android:state_pressed="true" android:drawable="@drawable/tab_bg_selected" />
<!-- Selected tab (using d-pad) -->
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
</selector>
--- TAB SELECED ---
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F"
android:endColor="#696969" android:angle="-90" />
</shape>
--- TAB UNSELECTED ---
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#5C5C5C" android:centerColor="#424242"
android:endColor="#222222" android:angle="-90" />
</shape>
And set the colors to whatever you want, good luck! Oh I almost forgot, also when setting the Intent in tabhost, use the setupTab method like this:
Intent i = new Intent(this, AAACalendarActivity.class);
setupTab(new TextView(this), "Month", i);
i = new Intent(this, AAACalendarWeekActivity.class);
setupTab(new TextView(this), "Week", i);
i = new Intent(this, AAACalendarDayActivity.class);
setupTab(new TextView(this), "Day", i);
Upvotes: 0
Reputation: 28093
On clicking any of them, i want the tab to be focussed and of a different color than the other one.
You can implement it as follows.
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String str) {
tabHost.getCurrentTabView().setBackgroundColor(color);
tabHost.getCurrentTabView().setBackgroundDrawable(drawable);
tabHost.getCurrentTabView().setBackgroundResource(resid);
}
});
Upvotes: 4