Ritesh Kumar Gupta
Ritesh Kumar Gupta

Reputation: 5191

Android- Create tabs inside fragment without v4 support library

I am trying to create a 'tabs' w/o using support library for my application, but I didn't find even a simple working example/tutorial anywhere.

Since my entire application is using android.app.fragment, so I cannot change all of the existing fragments to android.support.v4.app.Fragment.

Creating a tab within fragment using support library is a piece of cake, but I am wondering whether we can create one w/o android.support.v4.app.Fragment.

Upvotes: 1

Views: 1476

Answers (2)

codezjx
codezjx

Reputation: 9142

This can be achieved, it's easily, follow my step:

1.Copy the offical source code of FragmentTabHost to your project, and change following 3 class refrence: android.support.v4.app.Fragment to android.app.Fragment android.support.v4.app.FragmentManager to android.app.FragmentManager android.support.v4.app.FragmentTransaction to android.app.FragmentTransaction

2.Use the modify version of your FragmentTabHost in project, and forget the android.support.v4 library. My edited version: FragmentTabHost

Sample code: (Show the first tab, after 5 sec delay, you will see the second tab, I run on my Android Studio successfully!)

MainActivity.java

public class MainActivity extends Activity {

    FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
        mTabHost.setup(this, getFragmentManager(), R.id.container);

        // Add each tab
        mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("first"), BlankFragment1.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("second"), BlankFragment2.class, null);


        mTabHost.postDelayed(new Runnable() {
            @Override
            public void run() {
                mTabHost.setCurrentTabByTag("second");
            }
        }, 5000);
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <com.example.bill.myapplication.FragmentTabHost
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:id="@+id/tabhost">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TabWidget
                android:id="@android:id/tabs"
                android:orientation="horizontal"
                android:layout_width="0dp"
                android:layout_height="0dp"/>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"/>

            <FrameLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
        </LinearLayout>
    </com.example.bill.myapplication.FragmentTabHost>

</RelativeLayout>

Upvotes: 3

StenSoft
StenSoft

Reputation: 9617

Put the fragments inside tab content in your layout XML and add them to the TabHost in onCreate.

Upvotes: 0

Related Questions