{"id":11582,"date":"2014-07-17T11:00:44","date_gmt":"2014-07-17T08:00:44","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=11582"},"modified":"2019-05-09T11:09:53","modified_gmt":"2019-05-09T08:09:53","slug":"android-fragment-tabs-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/","title":{"rendered":"Android Fragment Tabs Example"},"content":{"rendered":"<p>As title clarifies, today we &#8216;re gonna see how to implement a <code>Tab Layout<\/code> using <a href=\"http:\/\/developer.android.com\/guide\/components\/fragments.html\" target=\"_blank\" rel=\"noopener noreferrer\"><code>Fragments<\/code><\/a>. Regarding the previous versions of Android, a <code>Tab Layout<\/code> could be implemented using <code>TabWidget<\/code> and <code>TabHost<\/code>, but now, these are deprecated, so we have to find another workaround, if we don&#8217;t want to directly use Google&#8217;s <a href=\"http:\/\/developer.android.com\/reference\/android\/app\/TabActivity.html\" target=\"_blank\" rel=\"noopener noreferrer\">solution<\/a>.<\/p>\n<p>So, today, another easy example: we &#8216;ll create three (3) tabs (they correspond to my most-driven cars), which will contain fragments; Android <code>Fragments<\/code> must be setup using <code>Listeners<\/code> &#8211; you &#8216;ll see what I mean while moving on.<\/p>\n<p>For the need of this example, we will use the following tools in a Windows 64-bit platform:<\/p>\n<ol>\n<li>JDK 1.7<\/li>\n<li>Eclipse 4.4 Luna<\/li>\n<li>Android SDK 4.4<\/li>\n<\/ol>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip project creation and jump directly to the <a href=\"#code\"><strong>beginning of the example<\/strong><\/a> below.<\/div>\n<\/p>\n<h2>1. Creating a New Android Application Project<\/h2>\n<p>Open Eclipse IDE and go to File \u2192 New \u2192 Project \u2192 Android Application Project.<\/p>\n<p>Fill in the name of the application, the project and the package in the appropriate fields and then click Next.<\/p>\n<p><figure id=\"attachment_11589\" aria-describedby=\"caption-attachment-11589\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project_11.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project_11.png\" alt=\"Figure 1. Create a new Android application\" width=\"636\" height=\"543\" class=\"size-full wp-image-11589\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project_11.png 636w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project_11-300x256.png 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/a><figcaption id=\"caption-attachment-11589\" class=\"wp-caption-text\">Figure 1. Create a new Android application<\/figcaption><\/figure><\/p>\n<p>In the next window, the \u201cCreate Activity\u201d option should be checked. The new created activity will be the main activity of your project. Then press Next button.<\/p>\n<p><figure id=\"attachment_11590\" aria-describedby=\"caption-attachment-11590\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project_21.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project_21.png\" alt=\"Figure 2. Configure the project\" width=\"636\" height=\"543\" class=\"size-full wp-image-11590\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project_21.png 636w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project_21-300x256.png 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/a><figcaption id=\"caption-attachment-11590\" class=\"wp-caption-text\">Figure 2. Configure the project<\/figcaption><\/figure><\/p>\n<p>In \u201cConfigure Launcher Icon\u201d window you should choose the icon you want to have in your app. We will use the default icon of android, so click Next button.<\/p>\n<p><figure id=\"attachment_11468\" aria-describedby=\"caption-attachment-11468\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_3.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_3.png\" alt=\"Figure 3. Configure the launcher icon\" width=\"660\" height=\"590\" class=\"size-full wp-image-11468\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_3.png 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_3-300x268.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-11468\" class=\"wp-caption-text\">Figure 3. Configure the launcher icon<\/figcaption><\/figure><\/p>\n<p>Select the \u201cBlank Activity\u201d option and press Next.<\/p>\n<p><figure id=\"attachment_11469\" aria-describedby=\"caption-attachment-11469\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_4.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_4.png\" alt=\"Figure 4. Create the activity and select its type\" width=\"660\" height=\"590\" class=\"size-full wp-image-11469\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_4.png 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_4-300x268.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-11469\" class=\"wp-caption-text\">Figure 4. Create the activity and select its type<\/figcaption><\/figure><\/p>\n<p>You then have to specify a name for the new Activity and a name for the layout description of your app. The .xml files for the layout will automatically be created in the <code>res\/layout<\/code> folder. Finally, press Finish.<\/p>\n<p><figure id=\"attachment_11470\" aria-describedby=\"caption-attachment-11470\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_5.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_5.png\" alt=\"Figure 5. Create a new blank activity\" width=\"660\" height=\"590\" class=\"size-full wp-image-11470\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_5.png 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_5-300x268.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-11470\" class=\"wp-caption-text\">Figure 5. Create a new blank activity<\/figcaption><\/figure><\/p>\n<p>Here is the very final structure of the project, just in case you &#8216;ll miss something, while developing:<\/p>\n<p><figure id=\"attachment_11592\" aria-describedby=\"caption-attachment-11592\" style=\"width: 368px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project_structure1.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project_structure1.png\" alt=\"Figure 6. The final structure of the project\" width=\"368\" height=\"544\" class=\"size-full wp-image-11592\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project_structure1.png 368w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project_structure1-202x300.png 202w\" sizes=\"(max-width: 368px) 100vw, 368px\" \/><\/a><figcaption id=\"caption-attachment-11592\" class=\"wp-caption-text\">Figure 6. The final structure of the project<\/figcaption><\/figure><\/p>\n<h2><span id=\"code\">2. Creating the layout of the Main Activity<\/span><\/h2>\n<p>Let&#8217;s do it by opening <code>res\/layout\/activity_main.xml<\/code> and navigating to the respective xml tab and paste the following.<\/p>\n<p><span style=\"text-decoration: underline\"><i><code>activity_main.xml<\/code><\/i><\/span><\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;FrameLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:id=\"@+id\/activity_main\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\" \/&gt;\n<\/pre>\n<p>This is about the main screen that our tabs will be hosted on; there come&#8217;s the <code>ActionListener<\/code> that I fore-mentioned. Especially, we here deal with <a href=\"http:\/\/developer.android.com\/reference\/android\/app\/ActionBar.TabListener.html\" target=\"_blank\" rel=\"noopener noreferrer\"><code>ActionBar.TabListener<\/code><\/a>.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>We &#8216;ll override its methods, but we don&#8217;t actually need the first parameter of them ( <code>tab<\/code> ), as we &#8216;ll use <code>Fragment<\/code> elements. Doing so, means that each time that our <code>TabListener<\/code> is called, a user action occurs (in our case, the only user action is the tabs&#8217; transition), so we have to conform our <code>TabListener<\/code> in order to automatically change the layout, according to the selected tab.<\/p>\n<p>Each tab&#8217;s content will be in a separate layout file (so, three extra in overall, one for each tab), but the actual transition will be implemented in the <code>MainActivity<\/code>, using TabListeners  for each tab.<\/p>\n<p><i>I &#8216;ll leave the rest of your &#8220;gaps&#8221; to be filled, while reading <code>TabListener<\/code>&#8216;s comments; you &#8216;ll find this way more mind-sharpening.<\/i><\/p>\n<p><i>But why to use a <code>FrameLayout<\/code>?<\/i><br \/>\n<a href=\"http:\/\/blog.neteril.org\/blog\/2013\/10\/10\/framelayout-your-best-ui-friend\/\" target=\"_blank\" rel=\"noopener noreferrer\">This<\/a> and <a href=\"http:\/\/stackoverflow.com\/questions\/19453530\/android-when-why-should-i-use-framelayout-instead-of-fragment\" target=\"_blank\" rel=\"noopener noreferrer\">this<\/a> will resolve this issue, too.<\/p>\n<h2>3. Coding the Main Activity<\/h2>\n<p>Let&#8217;s first navigate to <code>src\/com.javacodegeeks.android.textviewtest\/MainActivity.java<\/code>. Next, complete the class like this:<\/p>\n<p><span style=\"text-decoration: underline\"><i><code>MainActivity.java<\/code><\/i><\/span><\/p>\n<pre class=\"brush:java; wrap-lines:false\">package com.javacodegeeks.android.tablayout_with_fragments;\n\nimport android.app.ActionBar;\nimport android.app.Activity;\nimport android.app.Fragment;\nimport android.os.Bundle;\n\npublic class MainActivity extends Activity {\n\n\t\/\/ Declaring our tabs and the corresponding fragments.\n\tActionBar.Tab bmwTab, fordTab, toyotaTab;\n\tFragment bmwFragmentTab = new BmwFragmentTab();\n\tFragment toyotaFragmentTab = new ToyotaFragmentTab();\n\tFragment fordFragmentTab = new FordFragmentTab();\n\t\n\t@Override\n\tprotected void onCreate(Bundle savedInstanceState) {\n\t\tsuper.onCreate(savedInstanceState);\n\t\tsetContentView(R.layout.activity_main);\n\t\t\n\t\t\/\/ Asking for the default ActionBar element that our platform supports.\n\t\tActionBar actionBar = getActionBar();\n\t\t \n        \/\/ Screen handling while hiding ActionBar icon.\n        actionBar.setDisplayShowHomeEnabled(false);\n \n        \/\/ Screen handling while hiding Actionbar title.\n        actionBar.setDisplayShowTitleEnabled(false);\n \n        \/\/ Creating ActionBar tabs.\n        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);\n \n        \/\/ Setting custom tab icons.\n        bmwTab = actionBar.newTab().setIcon(R.drawable.bmw_logo);\n        toyotaTab = actionBar.newTab().setIcon(R.drawable.toyota_logo);\n        fordTab = actionBar.newTab().setIcon(R.drawable.ford_logo);\n        \n        \/\/ Setting tab listeners.\n        bmwTab.setTabListener(new TabListener(bmwFragmentTab));\n        toyotaTab.setTabListener(new TabListener(toyotaFragmentTab));\n        fordTab.setTabListener(new TabListener(fordFragmentTab));\n       \n        \/\/ Adding tabs to the ActionBar.\n        actionBar.addTab(bmwTab);\n        actionBar.addTab(toyotaTab);\n        actionBar.addTab(fordTab);\n\t}\n}\n<\/pre>\n<p>Let&#8217;s stick to the difficult part of it: line 11 declares three <code>ActionBar<\/code> tabs, while in lines 39-41, a <code>TabListener<\/code> is assigned to each one of them, according to the specific <code>Fragment<\/code> that it corresponds to (lines 12-14). What lines 39-41 also make clear is, that we have to code a <code>constructor<\/code>, for the <code>TabListener<\/code> class.<\/p>\n<p>In addition to the fore-mentioned class, we also have to write three extra classes, as lines 12-14 clarify.<\/p>\n<h2>4. Coding the &#8220;extra&#8221; Classes<\/h2>\n<p>Let&#8217;s start by writing the <code>TabListener<\/code> class.<\/p>\n<p><span style=\"text-decoration: underline\"><i><code>TabListener.java<\/code><\/i><\/span><\/p>\n<pre class=\"brush:java; wrap-lines:false\">package com.javacodegeeks.android.tablayout_with_fragments;\n\nimport android.app.ActionBar.Tab;\nimport android.app.Fragment;\nimport android.app.FragmentTransaction;\nimport android.app.ActionBar;\n\npublic class TabListener implements ActionBar.TabListener {\n\n\tprivate Fragment fragment;\n\t\n\t\/\/ The contructor.\n\tpublic TabListener(Fragment fragment) {\n\t\tthis.fragment = fragment;\n\t}\n\n\t\/\/ When a tab is tapped, the FragmentTransaction replaces\n\t\/\/ the content of our main layout with the specified fragment;\n\t\/\/ that's why we declared an id for the main layout.\n\t@Override\n\tpublic void onTabSelected(Tab tab, FragmentTransaction ft) {\n\t\tft.replace(R.id.activity_main, fragment);\n\t}\n\n\t\/\/ When a tab is unselected, we have to hide it from the user's view. \n\t@Override\n\tpublic void onTabUnselected(Tab tab, FragmentTransaction ft) {\n\t\tft.remove(fragment);\n\t}\n\n\t\/\/ Nothing special here. Fragments already did the job.\n\t@Override\n\tpublic void onTabReselected(Tab tab, FragmentTransaction ft) {\n\t\t\n\t}\n}\n<\/pre>\n<p>We &#8216;ll continue by adding the classes, that refer to the make&#8217;s FragmentTab.[ulp id=&#8217;XSQzgZ7lDbTb31Tj&#8217;]<\/p>\n<p><span style=\"text-decoration: underline\"><i><code>BmwFragmentTab.java<\/code><\/i><\/span><\/p>\n<pre class=\"brush:java; wrap-lines:false\">package com.javacodegeeks.android.tablayout_with_fragments;\n\nimport android.os.Bundle;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.view.ViewGroup;\nimport android.app.Fragment;\n \npublic class BmwFragmentTab extends Fragment {\n    @Override\n    public View onCreateView(LayoutInflater inflater, ViewGroup container,\n            Bundle savedInstanceState) {\n        View rootView = inflater.inflate(R.layout.bmw_layout, container, false);\n        return rootView;\n    }\n}\n<\/pre>\n<p>What we accomplished here is creating an independent <code>Fragment<\/code> tab (regarding our BMW category), whom&#8217;s <code>onCreate<\/code> method results to the corresponding xml layout (a simple <code>TextView<\/code> that contains my personal experience, according to the specified make).<\/p>\n<p><span style=\"text-decoration: underline\"><i><code>ToyotaFragmentTab.java<\/code><\/i><\/span><\/p>\n<pre class=\"brush:java; wrap-lines:false\">package com.javacodegeeks.android.tablayout_with_fragments;\n\nimport android.os.Bundle;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.view.ViewGroup;\nimport android.app.Fragment;\n \npublic class ToyotaFragmentTab extends Fragment {\n    @Override\n    public View onCreateView(LayoutInflater inflater, ViewGroup container,\n            Bundle savedInstanceState) {\n        View rootView = inflater.inflate(R.layout.toyota_layout, container, false);\n        return rootView;\n    }\n}\n<\/pre>\n<p>Continuing on the same concept with <code>BmwFragmentTab<\/code>, we here point the <code>ToyotaFragmentTab<\/code> to the <code>toyota_layout<\/code> (no need for worries till here, as this is about simple layouts, as I fore-mentioned, which we &#8216;ll code in the next paragraph).<\/p>\n<p><span style=\"text-decoration: underline\"><i><code>FordFragmentTab.java<\/code><\/i><\/span><\/p>\n<pre class=\"brush:java; wrap-lines:false\">package com.javacodegeeks.android.tablayout_with_fragments;\n\nimport android.os.Bundle;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.view.ViewGroup;\nimport android.app.Fragment;\n \npublic class FordFragmentTab extends Fragment {\n    @Override\n    public View onCreateView(LayoutInflater inflater, ViewGroup container,\n            Bundle savedInstanceState) {\n        View rootView = inflater.inflate(R.layout.ford_layout, container, false);\n        return rootView;\n    }\n}\n<\/pre>\n<p>Same here, a <code>FragmentTab<\/code> that points the layout we need.<\/p>\n<h2>5. Coding the &#8220;extra&#8221; layouts<\/h2>\n<p>What is clear from the previous paragraph, is the fact that we need three xml layouts, to host our FragmentTabs&#8217; user interface. A simple <code>TextView<\/code> for each one, as I said.<\/p>\n<p><span style=\"text-decoration: underline\"><i><code>bmw_layout.xml<\/code><\/i><\/span><\/p>\n<pre class=\"brush:xml\">&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\" &gt;\n \n    &lt;TextView\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_centerHorizontal=\"true\"\n        android:layout_centerVertical=\"true\"\n        android:text=\"Yes, I'm a proud E46-er!\" \/&gt;\n \n&lt;\/RelativeLayout&gt;\n<\/pre>\n<p><span style=\"text-decoration: underline\"><i><code>toyota_layout.xml<\/code><\/i><\/span><\/p>\n<pre class=\"brush:xml\">&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\" &gt;\n \n    &lt;TextView\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_centerHorizontal=\"true\"\n        android:layout_centerVertical=\"true\"\n        android:text=\"You mud? Toyota Hilux 2.4TD!\" \/&gt;\n \n&lt;\/RelativeLayout&gt;\n<\/pre>\n<p><span style=\"text-decoration: underline\"><i><code>ford_layout.xml<\/code><\/i><\/span><\/p>\n<pre class=\"brush:xml\">&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\" &gt;\n \n    &lt;TextView\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_centerHorizontal=\"true\"\n        android:layout_centerVertical=\"true\"\n        android:text=\"God, help the poor man that bought my Fiesta.\" \/&gt;\n \n&lt;\/RelativeLayout&gt;\n<\/pre>\n<h2>6. Running the application<\/h2>\n<p><figure id=\"attachment_11626\" aria-describedby=\"caption-attachment-11626\" style=\"width: 488px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_bmw.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_bmw.png\" alt=\"Figure 7. Initial state of the app - first tab is selected by default\" width=\"488\" height=\"470\" class=\"size-full wp-image-11626\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_bmw.png 488w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_bmw-300x288.png 300w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/a><figcaption id=\"caption-attachment-11626\" class=\"wp-caption-text\">Figure 7. Initial state of the app &#8211; first tab is selected by default<\/figcaption><\/figure><\/p>\n<p>Let&#8217;s tap the other two tabs, too!<\/p>\n<p><figure id=\"attachment_11627\" aria-describedby=\"caption-attachment-11627\" style=\"width: 488px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_toyota.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_toyota.png\" alt=\"Figure 8. Tapping the second tab\" width=\"488\" height=\"470\" class=\"size-full wp-image-11627\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_toyota.png 488w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_toyota-300x288.png 300w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/a><figcaption id=\"caption-attachment-11627\" class=\"wp-caption-text\">Figure 8. Tapping the second tab<\/figcaption><\/figure><br \/>\n<figure id=\"attachment_11628\" aria-describedby=\"caption-attachment-11628\" style=\"width: 488px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_ford.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_ford.png\" alt=\"Figure 9. Tapping the third tab\" width=\"488\" height=\"470\" class=\"size-full wp-image-11628\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_ford.png 488w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_ford-300x288.png 300w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/a><figcaption id=\"caption-attachment-11628\" class=\"wp-caption-text\">Figure 9. Tapping the third tab<\/figcaption><\/figure><\/p>\n<h2>7. Source Code Download<\/h2>\n<p>Cheers!<br \/>\nThis was an example of Fragment Tabs in Android. You can also download the source code for this example: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/TabLayout_with_Fragments.zip\">TabLayout_with_Fragments.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As title clarifies, today we &#8216;re gonna see how to implement a Tab Layout using Fragments. Regarding the previous versions of Android, a Tab Layout could be implemented using TabWidget and TabHost, but now, these are deprecated, so we have to find another workaround, if we don&#8217;t want to directly use Google&#8217;s solution. So, today, &hellip;<\/p>\n","protected":false},"author":12,"featured_media":1202,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[489],"tags":[1156],"class_list":["post-11582","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fragment","tag-eclipse"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android Fragment Tabs Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"As title clarifies, today we &#039;re gonna see how to implement a Tab Layout using Fragments. Regarding the previous versions of Android, a Tab Layout could\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android Fragment Tabs Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"As title clarifies, today we &#039;re gonna see how to implement a Tab Layout using Fragments. Regarding the previous versions of Android, a Tab Layout could\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/toubou.techblog\/\" \/>\n<meta property=\"article:published_time\" content=\"2014-07-17T08:00:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-09T08:09:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Thodoris Bais\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ThodorisBais\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Thodoris Bais\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/\"},\"author\":{\"name\":\"Thodoris Bais\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/045f5a70ed608824c16959f146ede983\"},\"headline\":\"Android Fragment Tabs Example\",\"datePublished\":\"2014-07-17T08:00:44+00:00\",\"dateModified\":\"2019-05-09T08:09:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/\"},\"wordCount\":850,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"keywords\":[\"Eclipse\"],\"articleSection\":[\"Fragment\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/\",\"name\":\"Android Fragment Tabs Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"datePublished\":\"2014-07-17T08:00:44+00:00\",\"dateModified\":\"2019-05-09T08:09:53+00:00\",\"description\":\"As title clarifies, today we 're gonna see how to implement a Tab Layout using Fragments. Regarding the previous versions of Android, a Tab Layout could\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"core\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"app\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/app\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Fragment\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/app\/fragment\/\"},{\"@type\":\"ListItem\",\"position\":6,\"name\":\"Android Fragment Tabs Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/045f5a70ed608824c16959f146ede983\",\"name\":\"Thodoris Bais\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/Thodoris-Bais_avatar_1425256492-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/Thodoris-Bais_avatar_1425256492-96x96.jpg\",\"caption\":\"Thodoris Bais\"},\"description\":\"Thodoris is an Oracle Certified Associate Java Programmer and currently works as a Junior Software Developer, for Intrasoft International S.A. He holds a diploma at Informatics &amp; Telecommunications Engineering and is interested in continuous development.\",\"sameAs\":[\"http:\/\/thodorisbais.blogspot.com\",\"https:\/\/www.facebook.com\/toubou.techblog\/\",\"https:\/\/instagram.com\/thodoris.bais\/\",\"https:\/\/www.linkedin.com\/pub\/thodoris-bais\/69\/6b7\/408\",\"https:\/\/x.com\/@ThodorisBais\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/thodoris-bais\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android Fragment Tabs Example - Java Code Geeks","description":"As title clarifies, today we 're gonna see how to implement a Tab Layout using Fragments. Regarding the previous versions of Android, a Tab Layout could","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/","og_locale":"en_US","og_type":"article","og_title":"Android Fragment Tabs Example - Java Code Geeks","og_description":"As title clarifies, today we 're gonna see how to implement a Tab Layout using Fragments. Regarding the previous versions of Android, a Tab Layout could","og_url":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/www.facebook.com\/toubou.techblog\/","article_published_time":"2014-07-17T08:00:44+00:00","article_modified_time":"2019-05-09T08:09:53+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","type":"image\/jpeg"}],"author":"Thodoris Bais","twitter_card":"summary_large_image","twitter_creator":"@ThodorisBais","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Thodoris Bais","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/"},"author":{"name":"Thodoris Bais","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/045f5a70ed608824c16959f146ede983"},"headline":"Android Fragment Tabs Example","datePublished":"2014-07-17T08:00:44+00:00","dateModified":"2019-05-09T08:09:53+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/"},"wordCount":850,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","keywords":["Eclipse"],"articleSection":["Fragment"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/","url":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/","name":"Android Fragment Tabs Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","datePublished":"2014-07-17T08:00:44+00:00","dateModified":"2019-05-09T08:09:53+00:00","description":"As title clarifies, today we 're gonna see how to implement a Tab Layout using Fragments. Regarding the previous versions of Android, a Tab Layout could","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragment-tabs-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Android","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/"},{"@type":"ListItem","position":3,"name":"core","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/"},{"@type":"ListItem","position":4,"name":"app","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/app\/"},{"@type":"ListItem","position":5,"name":"Fragment","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/app\/fragment\/"},{"@type":"ListItem","position":6,"name":"Android Fragment Tabs Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/045f5a70ed608824c16959f146ede983","name":"Thodoris Bais","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/Thodoris-Bais_avatar_1425256492-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/Thodoris-Bais_avatar_1425256492-96x96.jpg","caption":"Thodoris Bais"},"description":"Thodoris is an Oracle Certified Associate Java Programmer and currently works as a Junior Software Developer, for Intrasoft International S.A. He holds a diploma at Informatics &amp; Telecommunications Engineering and is interested in continuous development.","sameAs":["http:\/\/thodorisbais.blogspot.com","https:\/\/www.facebook.com\/toubou.techblog\/","https:\/\/instagram.com\/thodoris.bais\/","https:\/\/www.linkedin.com\/pub\/thodoris-bais\/69\/6b7\/408","https:\/\/x.com\/@ThodorisBais"],"url":"https:\/\/examples.javacodegeeks.com\/author\/thodoris-bais\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/11582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=11582"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/11582\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1202"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=11582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=11582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=11582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}