Search code examples
javaandroidandroid-fragmentsandroid-tabhostfragment-tab-host

Swipe effect in TabHost inside a fragment


I have a NavigationDrawer that opens fragments if a button is clicked. But now, i need to make a Tabhost in one of these fragments. I never worked with TabHost and i can't find any topic explaining how to implement the Swipe effect in one TabHost inside a fragment. Until then, i have the Tabhost configured but don't have any effect. I'll be thankful if you can explain me how to do it.

PacksFragment.java

package studio.com.archeagemanager;


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.TabHost;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 */


public class PacksFragment extends Fragment {

    TabHost tabHost;

    public PacksFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_packs, container, false);

        TabHost host = (TabHost) view.findViewById(R.id.tabhost);
        host.setup();

        //Tab 1
        TabHost.TabSpec spec = host.newTabSpec("Tab One");
        spec.setContent(R.id.nuia);
        spec.setIndicator("NUIA");
        host.addTab(spec);

        //Tab 2
        spec = host.newTabSpec("Tab two");
        spec.setContent(R.id.haranya);
        spec.setIndicator("HARANYA");
        host.addTab(spec);

        //Tab 3
        spec = host.newTabSpec("Tab three");
        spec.setContent(R.id.oceanic);
        spec.setIndicator("OCEANIC");
        host.addTab(spec);

        //Tab 4
        spec = host.newTabSpec("Tab four");
        spec.setContent(R.id.auroria);
        spec.setIndicator("AURORIA");
        host.addTab(spec);

        for (int i=0; i<4; i++) {
            View tabView = host.getTabWidget().getChildTabViewAt(i);
            tabView.setPadding(0, 0, 0, 0);
            TextView tv = (TextView)tabView.findViewById(android.R.id.title);
            tv.setSingleLine();
            tv.setTextSize(14);
        }

        return view;
    }

}

Solution

  • In Your Layout file fragments_pack Just add Tab Host with Horizontal Scroll View as per your requirement similar to this

    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
    
        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/horizontalScrollView"
            android:fillViewport="true"
            android:scrollbars="none">
    
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </HorizontalScrollView>
    
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>