TabHost does not appear on the screen

I'm trying to use TabHost in my application and I just dragged it into my activity using design, but when I launched it, it just won’t appear, just get a white screen, does anyone know why?

 <TabHost android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tabHost" android:layout_gravity="center_horizontal"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"></TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/tab1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"></LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"></LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"></LinearLayout> </FrameLayout> </LinearLayout> </TabHost> 

+6
source share
1 answer

This is simply because you simply cannot create a TabHost using only XML code. You need to add TabSpec to TabHost as follows:

 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); 
+10
source

Source: https://habr.com/ru/post/981092/


All Articles