Reputation: 17340
I am using tab host to show tabs, but even when I simply drag and drop it on my layout, and run it, its not showing the tabs, its showing white screen,I guess which is of the linear layout of first tab view.
<?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="@android: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" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
When I run above on emulator or phone (Huawei Ascend P1), no tabs are shown.
Upvotes: 1
Views: 8145
Reputation: 1789
This answer is for the people, like myself, who started using Kotlin for Android development. The following code used three ids that you need to match it to your view's XML file. First as stated in the library:
Call setup() before adding tabs if loading TabHost using findViewById(). However: You do not need to call setup() after getTabHost() in TabActivity.
//Your tab host id.
yourTabHostId.setup()
//Need to use var as we will reassign it for the second tab.
//First tab is a tag and must be defined.
var tabSpec = yourTabHostId.newTabSpec("First Tab")
//Use the id for the first tab, for the content of your first tab.
tabSpec.setContent(R.id.firstTab)
tabSpec.setIndicator("First Tab")
yourTabHostId.addTab(tabSpec)
tabSpec = yourTabHostId.newTabSpec("Second Tab")
//Use the id for your second tab, for the content of your second tab.
tabSpec.setContent(R.id.secondTab)
tabSpec.setIndicator("Second Tab")
yourTabHostId.addTab(tabSpec)
Upvotes: 0
Reputation: 182
This is happening simply because you just can't create TabHost using xml code only. You need to add TabSpecs to the TabHost like this:
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabSpec tab3 = tabHost.newTabSpec("Third Tab");
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,TabActivity1.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,TabActivity2.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this,TabActivity3.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
Upvotes: 5