Fragment TabHost Example in android

In this tutorial, you will learn how to implement FragmentTabHost in your Android application. FragmentTabHost allows the use of fragment objects for its tab content. We will create fragment tabs with FragmentTabHost using on tab selection will show a selected fragment. A partir del nivel de API 13, TabHost ha sido declarado como obsoleto. Google ha reorientado su jerarquía de clases para introducir el concepto de fragment, de manera que en lugar de TabHost, propone utilizar FragmentTabHost. No obstante, podemos seguir utilizando TabHost sin ningún problema.

1. HomeActivity.java

package com.tutorialsee.fragmenttabhostdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;

public class HomeActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.homeactivity_tabs);

mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//TabHost color change this code
//mTabHost.getChildAt(0).setBackgroundColor(Color.parseColor("#C4996C"));

Bundle b = new Bundle();
b.putString("key", "Windows");
mTabHost.addTab(mTabHost.newTabSpec("windows").setIndicator("Windows"),Fragment1.class, b);

b = new Bundle();
System.out.print("hello git");
b.putString("key", "IOS");
mTabHost.addTab(mTabHost.newTabSpec("ios").setIndicator("IOS"), Fragment2.class, b);

b = new Bundle();
b.putString("key", "Samsung");
mTabHost.addTab(mTabHost.newTabSpec("samsung").setIndicator("Samsung"),Fragment3.class, b);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

2. homeactivity_tabs.xml

<?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" >

    <FrameLayout
        android:id="@+id/realtabcontent"
        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" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>