Android Tabs Example - Android

How to create tabs in android?

Snippet Code

Rate this page :
  [ 0 votes]

Android tabs are used to create tabs layout. The steps to create tabs in android is given below.
Add an element to your XML layout which is used to setup your layout your page viewer.
Then create layout contains the individual design for the Tabs.
Write down the design code in
Add navigation coding in ------------------- package; import; import; import; import android.os.Bundle; import; import; public class MainActivity extends FragmentActivity implements ActionBar.TabListener { private ViewPager viewPager; private ActionBar actionBar; private TabPagerAdapter tabPagerAdapter; private String[] tabs = { "Missed Calls", "Dialled", "Received" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(; tabPagerAdapter = new TabPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(tabPagerAdapter); actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); for (String tab_name : tabs) { actionBar.addTab(actionBar.newTab().setText(tab_name) .setTabListener(this)); } viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { /** * on swipe select the respective tab * */ @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) {} } --------------------- @Override public Fragment getItem(int index) { Bundle bundle = new Bundle(); String tab = ""; int colorResId = 0; switch (index) { case 0: tab = "List of Missed Calls"; colorResId = R.color.color1; break; case 1: tab = "List of Dialled Calls"; colorResId = R.color.color2; break; case 2: tab = "List of Received Calls"; colorResId = R.color.color3; break; } bundle.putString("tab",tab); bundle.putInt("color", colorResId); SwipeTabFragment swipeTabFragment = new SwipeTabFragment(); swipeTabFragment.setArguments(bundle); return swipeTabFragment; } @Override public int getCount() { return 3; } }


Ask Questions

Ask Question