Search code examples
androidtabsandroid-tabhostswipe

Switch between tabs via swipe in android


I was create android application with tab and TabHost but users cant switch between tabs via swipe. I use TabActivity for this app. Users most click on tabs for switch between tabs or activities.

MainActivity.Java :

public class MainActivity extends TabActivity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Resources ressources = getResources(); 
    TabHost tabHost = getTabHost(); 

    //Home Intent
    Intent intentHome = new Intent().setClass(this, HomeActivity.class);
    TabSpec tabSpedHome = tabHost
            .newTabSpec("Home")
            .setIndicator("",ressources.getDrawable(R.drawable.icon_home_config))
            .setContent(intentHome);

    //Search Intent
    Intent intentSearch = new Intent().setClass(this, SearchActivity.class);
    TabSpec tabSpedSearch = tabHost
            .newTabSpec("Search")
            .setIndicator("",ressources.getDrawable(R.drawable.icon_search_config))
            .setContent(intentSearch);

    //Archive Intent
    Intent intentArchive = new Intent().setClass(this, ArchiveActivity.class);
    TabSpec tabSpedArchive = tabHost
            .newTabSpec("Archive")
            .setIndicator("",ressources.getDrawable(R.drawable.icon_archive_config))
            .setContent(intentArchive);

    //Download Intent
    Intent intentDownload = new Intent().setClass(this, DownloadActivity.class);
    TabSpec tabSpedDownload = tabHost
            .newTabSpec("Download")
            .setIndicator("",ressources.getDrawable(R.drawable.icon_download_config))
            .setContent(intentDownload);

    //Conctactus Intent
    Intent intentConctactus = new Intent().setClass(this, ContactusActivity.class);
    TabSpec tabSpedConctactus = tabHost
            .newTabSpec("Conctact us")
            .setIndicator("",ressources.getDrawable(R.drawable.icon_contactus_config))
            .setContent(intentConctactus);

    //AddAllTab
    tabHost.addTab(tabSpedHome);
    tabHost.addTab(tabSpedSearch);
    tabHost.addTab(tabSpedArchive);
    tabHost.addTab(tabSpedDownload);
    tabHost.addTab(tabSpedConctactus);

    //Set Default Tab
    tabHost.setCurrentTab(0);
}

}

And activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>

Solution

  • TabActivity has been deprecated for over three years. Please use another tab solution. Particularly if you want tab contents to be able to be swiped, use ViewPager for the tab contents and a tabbed indicator (e.g., PagerTabStrip) for the tabs.