I wrote a post about how to create and style the TabHost component in Android. Not surprising they have recently depreciated this widget in favor FragmentTabHost so I figured I’d run through the same examples as I did with TabHost just to document it. Most of this is taken from the SDK examples.
UPDATE: Since FragmentTabHost does not offer an icon option, I have set the icon in setIndicator(CharSequence label, Drawable icon) to ‘null’ in MainActivity.class.
ANDROID STUDIO: Android Studio does not provide dependencies so you will have to import them manually. File > Project Structure > app (under “Modules) > Dependenciestab. Click the “+” at the bottom to add a Library Dependency; in this case “support-v4“.
layout/activity_main.xml
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" 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="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> </android.support.v4.app.FragmentTabHost>
layout/fragment_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:background="#eaecee"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:text="@string/hello_world" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
MainActivity.java
import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; public class MainActivity extends FragmentActivity { private FragmentTabHost mTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); mTabHost.addTab( mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null), FragmentTab.class, null); mTabHost.addTab( mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null), FragmentTab.class, null); mTabHost.addTab( mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null), FragmentTab.class, null); } }
FragmentTab.java
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class FragmentTab extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_layout, container, false); TextView tv = (TextView) v.findViewById(R.id.text); tv.setText(this.getTag() + " Content"); return v; } }
UPDATE: Since I’ve been asked to give an example with the tabs on the bottom:
layout/activity_main.xml
<!--TABS WILL APPEAR ON THE BOTTOM--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1"/> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>