I have the following class in java. My purpose is to design a tabhost and have an activity start when the first tab is displayed. Also i want to have a different activity start when another tab is clicked. I have implemented the onTabChanged method but it doesn't seem to work. Can you help me?
Here is my class:
public class Tabs extends Activity implements OnTabChangeListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
TabHost th = (TabHost) findViewById(R.id.tabhost);
th.setup();
TabSpec profile = th.newTabSpec("tag1");
profile.setContent(R.id.tab1);
profile.setIndicator("", getResources().getDrawable(R.drawable.profile_tab));
TabSpec matches = th.newTabSpec("tag2");
matches.setContent(R.id.tab2);
matches.setIndicator("", getResources().getDrawable(R.drawable.matches_tab));
TabSpec friends = th.newTabSpec("tag3");
friends.setContent(R.id.tab3);
friends.setIndicator("", getResources().getDrawable(R.drawable.friends_tab));
th.addTab(profile);
th.addTab(matches);
th.addTab(friends);
}
@Override
public void onTabChanged(String arg0) {
// TODO Auto-generated method stub
}
}
This is my tabs.xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/activity_main"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/matches_page"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
you miss th.setOnTabChangeListener(this);
You have to inform TabHost
which implementation of the OnTabChangeListener
interface has to call