Tabbed Activity in Android Android by Rajesh Kumar Sahanee - May 29, 2018June 28, 20180 Post Views: 6,356 Hello Friends, It’s being long time I’ve not shared anything with you but today I have something to share which is Different types of Tabbed Activity in Android. In one of my application idea I needed to use tabbed activity, So i thought to share with you also, what different types of tabbed activity android provided. Actually there are three types of Tabbed Activity in Android or we can say three predefined tabbed activity template in android studio which are:- Swipe Views (ViewPager) Action Bar Tabs (with ViewPager) Action Bar Spinner But with few changes I have created another Tabbed Activity which is nothing but combination of Swipe Views and Action Bar Spinner. Actually in Swipe Views Tabbed Activity there is no way to see all tabs names just as in Action Bar Tabs or Action Bar Spinner, So I have combined Swipe Views with Action Bar Spinner, So that I can see tabs name and can change tab/page from spinner also. So you can find my Customised Tabbed Activity code below but for the other three predefined Tabbed Activity code you can download Android Studio Project below or You can find code in Android Studio itself also. SwipeViewWithSpinnerTabLayout.java SwipeViewWithSpinnerTabLayout Java package com.zatackcoder.tablayouttest; import android.content.Context; import android.content.res.Resources; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.ThemedSpinnerAdapter; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class SwipeViewWithSpinnerTabLayout extends AppCompatActivity { /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a * {@link FragmentPagerAdapter} derivative, which will keep every * loaded fragment in memory. If this becomes too memory intensive, it * may be best to switch to a * {@link android.support.v4.app.FragmentStatePagerAdapter}. */ private SectionsPagerAdapter mSectionsPagerAdapter; /** * The {@link ViewPager} that will host the section contents. */ private ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_swipe_view_with_spinner_tab_layout); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayShowTitleEnabled(false); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); // Setup spinner final Spinner spinner = (Spinner) findViewById(R.id.spinner); spinner.setAdapter(new SwipeViewWithSpinnerTabLayout.MyAdapter( toolbar.getContext(), new String[]{ "Section 1", "Section 2", "Section 3", })); // Change page when spinner selection changed spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { mViewPager.setCurrentItem(position); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); // Change spinner position when page change through slide mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){ @Override public void onPageSelected(int position) { spinner.setSelection(position); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_swipe_view_with_spinner_tab_layout, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private static class MyAdapter extends ArrayAdapter<String> implements ThemedSpinnerAdapter { private final ThemedSpinnerAdapter.Helper mDropDownHelper; public MyAdapter(Context context, String[] objects) { super(context, android.R.layout.simple_list_item_1, objects); mDropDownHelper = new ThemedSpinnerAdapter.Helper(context); } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { // Inflate the drop down using the helper's LayoutInflater LayoutInflater inflater = mDropDownHelper.getDropDownViewInflater(); view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false); } else { view = convertView; } TextView textView = (TextView) view.findViewById(android.R.id.text1); textView.setText(getItem(position)); return view; } @Override public Resources.Theme getDropDownViewTheme() { return mDropDownHelper.getDropDownViewTheme(); } @Override public void setDropDownViewTheme(Resources.Theme theme) { mDropDownHelper.setDropDownViewTheme(theme); } } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; public PlaceholderFragment() { } /** * Returns a new instance of this fragment for the given section * number. */ public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_swipe_view_with_spinner_tab_layout, container, false); TextView textView = (TextView) rootView.findViewById(R.id.section_label); textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); return rootView; } } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below). return PlaceholderFragment.newInstance(position + 1); } @Override public int getCount() { // Show 3 total pages. return 3; } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 package com.zatackcoder.tablayouttest; import android.content.Context;import android.content.res.Resources;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.ThemedSpinnerAdapter;import android.support.v7.widget.Toolbar;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.TextView; public class SwipeViewWithSpinnerTabLayout extends AppCompatActivity { /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a * {@link FragmentPagerAdapter} derivative, which will keep every * loaded fragment in memory. If this becomes too memory intensive, it * may be best to switch to a * {@link android.support.v4.app.FragmentStatePagerAdapter}. */ private SectionsPagerAdapter mSectionsPagerAdapter; /** * The {@link ViewPager} that will host the section contents. */ private ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_swipe_view_with_spinner_tab_layout); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayShowTitleEnabled(false); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); // Setup spinner final Spinner spinner = (Spinner) findViewById(R.id.spinner); spinner.setAdapter(new SwipeViewWithSpinnerTabLayout.MyAdapter( toolbar.getContext(), new String[]{ "Section 1", "Section 2", "Section 3", })); // Change page when spinner selection changed spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { mViewPager.setCurrentItem(position); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); // Change spinner position when page change through slide mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){ @Override public void onPageSelected(int position) { spinner.setSelection(position); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_swipe_view_with_spinner_tab_layout, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private static class MyAdapter extends ArrayAdapter<String> implements ThemedSpinnerAdapter { private final ThemedSpinnerAdapter.Helper mDropDownHelper; public MyAdapter(Context context, String[] objects) { super(context, android.R.layout.simple_list_item_1, objects); mDropDownHelper = new ThemedSpinnerAdapter.Helper(context); } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { // Inflate the drop down using the helper's LayoutInflater LayoutInflater inflater = mDropDownHelper.getDropDownViewInflater(); view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false); } else { view = convertView; } TextView textView = (TextView) view.findViewById(android.R.id.text1); textView.setText(getItem(position)); return view; } @Override public Resources.Theme getDropDownViewTheme() { return mDropDownHelper.getDropDownViewTheme(); } @Override public void setDropDownViewTheme(Resources.Theme theme) { mDropDownHelper.setDropDownViewTheme(theme); } } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; public PlaceholderFragment() { } /** * Returns a new instance of this fragment for the given section * number. */ public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_swipe_view_with_spinner_tab_layout, container, false); TextView textView = (TextView) rootView.findViewById(R.id.section_label); textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); return rootView; } } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below). return PlaceholderFragment.newInstance(position + 1); } @Override public int getCount() { // Show 3 total pages. return 3; } }} activity_swipe_view_with_spinner_tab_layout.xml activity_swipe_view_with_spinner_tab_layout.xml XHTML <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".SwipeViewWithSpinnerTabLayout"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/appbar_padding_top" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_weight="1" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/AppTheme.PopupOverlay" app:title="@string/app_name"> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout> 12345678910111213141516171819202122232425262728293031323334353637383940414243 <?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".SwipeViewWithSpinnerTabLayout"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/appbar_padding_top" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_weight="1" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/AppTheme.PopupOverlay" app:title="@string/app_name"> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout> fragment_swipe_view_with_spinner_tab_layout.xml fragment_swipe_view_with_spinner_tab_layout.xml XHTML <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/constraintLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SwipeViewWithSpinnerTabLayout$PlaceholderFragment"> <TextView android:id="@+id/section_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/activity_vertical_margin" android:layout_marginEnd="@dimen/activity_horizontal_margin" android:layout_marginStart="@dimen/activity_horizontal_margin" android:layout_marginTop="@dimen/activity_vertical_margin" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="@+id/constraintLayout" tools:layout_constraintLeft_creator="1" tools:layout_constraintTop_creator="1" /> </android.support.constraint.ConstraintLayout> 1234567891011121314151617181920212223 <?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/constraintLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SwipeViewWithSpinnerTabLayout$PlaceholderFragment"> <TextView android:id="@+id/section_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/activity_vertical_margin" android:layout_marginEnd="@dimen/activity_horizontal_margin" android:layout_marginStart="@dimen/activity_horizontal_margin" android:layout_marginTop="@dimen/activity_vertical_margin" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="@+id/constraintLayout" tools:layout_constraintLeft_creator="1" tools:layout_constraintTop_creator="1" /> </android.support.constraint.ConstraintLayout> menu_swipe_view_with_spinner_tab_layout.xml menu_swipe_view_with_spinner_tab_layout.xml XHTML <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.zatackcoder.tablayouttest.SwipeViewWithSpinnerTabLayout"> <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="@string/action_settings" app:showAsAction="never" /> </menu> 12345678910 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.zatackcoder.tablayouttest.SwipeViewWithSpinnerTabLayout"> <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="@string/action_settings" app:showAsAction="never" /></menu> Screenshots AndroidStudio Project Download Tabbed Activity in Android 1 file(s) 14.06 MB Download Thanks for stopping by, please share if you like it