Site icon C1CTech

Android Working With Tabs

<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Tabs<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Tabs<&sol;strong> <&sol;span>make it easy to explore and switch between different views&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>TabLayout<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>TabLayout<&sol;strong><&sol;span> provides a horizontal layout to display tabs&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">ViewPager Integration<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>If you&&num;8217&semi;re using a<span style&equals;"color&colon; &num;008000&semi;"><strong> ViewPager<&sol;strong><&sol;span> together with this layout&comma; you can call <strong><span style&equals;"color&colon; &num;008000&semi;">setupWithViewPager&lpar;ViewPager&rpar;<&sol;span><&sol;strong> to link the two together&period; This layout will be automatically populated from the <span style&equals;"color&colon; &num;008000&semi;"><strong>PagerAdapter&&num;8217&semi;s<&sol;strong><&sol;span> page titles&period;<&sol;p>&NewLine;<p>Get <strong><span style&equals;"color&colon; &num;0000ff&semi;">GITHUB<&sol;span> <&sol;strong>code from<span style&equals;"color&colon; &num;0000ff&semi;"> <a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;TabLayoutExample"><strong>Here<&sol;strong><&sol;a>&period;<&sol;span><&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"ok-jjDEKhBQ" title&equals;"Android Working With Tabs"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;ok-jjDEKhBQ"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;ok-jjDEKhBQ&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Working With Tabs"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Creating New Project<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>1 &period;In Android Studio&comma; go to <span style&equals;"color&colon; &num;008000&semi;"><strong>File <&sol;strong><strong>&Implies;<&sol;strong><strong> New Project<&sol;strong> <&sol;span>and fill all the details required to create a new project&period; When it prompts to select a default activity&comma; select <span style&equals;"color&colon; &num;008000&semi;"><strong>Blank Activity<&sol;strong><&sol;span> and proceed&period;<&sol;p>&NewLine;<p>2 &period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle&period; <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">Now <&sol;span><&sol;span>add <span style&equals;"color&colon; &num;000000&semi;">design support library <&sol;span><strong><span style&equals;"color&colon; &num;008000&semi;">com&period;android&period;support&colon;design&colon;26&period;1&period;0 <&sol;span><&sol;strong>and then sync the project&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Build&period;gradle<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>dependencies &lbrace;<&sol;code> <strong>     &NewLine;<span style&equals;"color&colon; &num;008000&semi;">compile 'com&period;android&period;support&colon;design&colon;26&period;1&period;0'<&sol;span><&sol;strong> &NewLine;&rcub;<&sol;pre>&NewLine;<p>3&period;Open <span style&equals;"color&colon; &num;008000&semi;"><strong>styles&period;xml<&sol;strong><&sol;span> located under <span style&equals;"color&colon; &num;008000&semi;"><strong>res <&sol;strong><strong>&Implies;<&sol;strong><strong> values<&sol;strong><&sol;span> and add below styles&period; The styles defined in this <strong><span style&equals;"color&colon; &num;008000&semi;">styles&period;xml<&sol;span><&sol;strong> are common to all the android versions&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>styles&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><strong>&lt&semi;resources&gt&semi;<&sol;strong>&NewLine;&NewLine;<strong> &lt&semi;&excl;-- Base application theme&period; --&gt&semi;<&sol;strong>&NewLine;<strong> &lt&semi;style name&equals;"AppTheme" parent&equals;"Theme&period;AppCompat&period;Light&period;DarkActionBar"&gt&semi;<&sol;strong>&NewLine;<strong> &lt&semi;&excl;-- Customize your theme here&period; --&gt&semi;<&sol;strong>&NewLine;<strong> &lt&semi;item name&equals;"colorPrimary"&gt&semi;&commat;color&sol;colorPrimary&lt&semi;&sol;item&gt&semi;<&sol;strong>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &lt&semi;item name&equals;"windowActionBar"&gt&semi;false&lt&semi;&sol;item&gt&semi;<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &lt&semi;item name&equals;"windowNoTitle"&gt&semi;true&lt&semi;&sol;item&gt&semi;<&sol;strong><&sol;span>&NewLine;<strong> &lt&semi;item name&equals;"colorPrimaryDark"&gt&semi;&commat;color&sol;colorPrimaryDark&lt&semi;&sol;item&gt&semi;<&sol;strong>&NewLine;<strong> &lt&semi;item name&equals;"colorAccent"&gt&semi;&commat;color&sol;colorAccent&lt&semi;&sol;item&gt&semi;<&sol;strong>&NewLine;<strong> &lt&semi;&sol;style&gt&semi;<&sol;strong>&NewLine;&NewLine;<strong>&lt&semi;&sol;resources&gt&semi;&NewLine;&NewLine;<&sol;strong><&sol;pre>&NewLine;<p>4&period;Under your <strong><span style&equals;"color&colon; &num;008000&semi;">main<&sol;span><&sol;strong> package create a fragment named <strong><span style&equals;"color&colon; &num;008000&semi;">M<&sol;span><span style&equals;"color&colon; &num;008000&semi;">oviesFragment&period;java<&sol;span> <&sol;strong>and add the below code&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MoviesFragment&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><strong>package com&period;example&period;lenovo&period;tablayout&period;fragment&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v4&period;app&period;Fragment&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;view&period;ViewGroup&semi;&NewLine;&NewLine;import com&period;example&period;lenovo&period;tablayout&period;R&semi;&NewLine;&NewLine;&NewLine;public class MoviesFragment extends Fragment&lbrace;&NewLine;&NewLine; &NewLine; &commat;Override&NewLine; public void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public View onCreateView&lpar;LayoutInflater inflater&comma; ViewGroup container&comma;&NewLine; Bundle savedInstanceState&rpar; &lbrace;&NewLine; &sol;&sol; Inflate the layout for this fragment&NewLine; return inflater&period;inflate&lpar;R&period;layout&period;fragment&lowbar;movies&comma; container&comma; false&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;strong>&NewLine;<&sol;pre>&NewLine;<p>5&period;Open <span style&equals;"color&colon; &num;008000&semi;"><strong>fragment&lowbar;movies&period;xml<&sol;strong><&sol;span> located under <span style&equals;"color&colon; &num;008000&semi;"><strong>res <&sol;strong><strong>&Implies;<&sol;strong><strong> layout<&sol;strong><&sol;span> and do the below changes&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>fragment&lowbar;movies&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><strong>&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;background&equals;"&commat;android&colon;color&sol;holo&lowbar;orange&lowbar;light"&NewLine; tools&colon;context&equals;"com&period;example&period;lenovo&period;tablayout&period;fragment&period;MoviesFragment"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"Movies"&NewLine; android&colon;textSize&equals;"40dp"&NewLine; android&colon;textStyle&equals;"bold"&NewLine; android&colon;layout&lowbar;centerInParent&equals;"true"&sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;strong>&NewLine;<&sol;pre>&NewLine;<p>6&period;Similarly&comma; again under your main package create a fragment named <strong><span style&equals;"color&colon; &num;008000&semi;">PlaysFragment&period;java&comma;EventsFragment&period;java<&sol;span><&sol;strong>  and add the below code&period;<br &sol;>&NewLine;Also&comma;open <span style&equals;"color&colon; &num;008000&semi;"><strong>fragment&lowbar;plays&period;xml<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>fragment&lowbar;events&period;xml <&sol;strong><&sol;span>located under <span style&equals;"color&colon; &num;008000&semi;"><strong>res <&sol;strong><strong>&Implies;<&sol;strong><strong> layout<&sol;strong> <&sol;span>and do the below changes&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>PlaysFragment&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<p><code><&sol;code><&sol;p>&NewLine;<pre><strong>package com&period;example&period;lenovo&period;tablayout&period;fragment&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v4&period;app&period;Fragment&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;view&period;ViewGroup&semi;&NewLine;&NewLine;import com&period;example&period;lenovo&period;tablayout&period;R&semi;&NewLine;&NewLine;&NewLine;public class PlaysFragment extends Fragment &lbrace;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; public void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public View onCreateView&lpar;LayoutInflater inflater&comma; ViewGroup container&comma;&NewLine; Bundle savedInstanceState&rpar; &lbrace;&NewLine; &sol;&sol; Inflate the layout for this fragment&NewLine; return inflater&period;inflate&lpar;R&period;layout&period;fragment&lowbar;plays&comma; container&comma; false&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;strong>&NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>EventsFragment&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><strong>package com&period;example&period;lenovo&period;tablayout&period;fragment&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v4&period;app&period;Fragment&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;view&period;ViewGroup&semi;&NewLine;&NewLine;import com&period;example&period;lenovo&period;tablayout&period;R&semi;&NewLine;&NewLine;&NewLine;public class EventsFragment extends Fragment&lbrace;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; public void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public View onCreateView&lpar;LayoutInflater inflater&comma; ViewGroup container&comma;&NewLine; Bundle savedInstanceState&rpar; &lbrace;&NewLine; &sol;&sol; Inflate the layout for this fragment&NewLine; return inflater&period;inflate&lpar;R&period;layout&period;fragment&lowbar;events&comma; container&comma; false&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;strong>&NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>fragment&lowbar;plays&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><strong>&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;background&equals;"&commat;android&colon;color&sol;holo&lowbar;blue&lowbar;light"&NewLine; tools&colon;context&equals;"com&period;example&period;lenovo&period;tablayout&period;fragment&period;PlaysFragment"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;centerInParent&equals;"true"&NewLine; android&colon;text&equals;"Plays"&NewLine; android&colon;textSize&equals;"40dp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;strong>&NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>fragment&lowbar;events&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre><strong>&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;background&equals;"&commat;android&colon;color&sol;holo&lowbar;green&lowbar;light"&NewLine; tools&colon;context&equals;"com&period;example&period;lenovo&period;tablayout&period;fragment&period;EventsFragment"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"Events"&NewLine; android&colon;textSize&equals;"40dp"&NewLine;&NewLine; android&colon;textStyle&equals;"bold"&NewLine; android&colon;layout&lowbar;centerInParent&equals;"true"&sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;&NewLine;<&sol;strong>&NewLine;<&sol;pre>&NewLine;<p>7&period;Open the layout file <strong><span style&equals;"color&colon; &num;008000&semi;">activity&lowbar;tab&period;xml<&sol;span><&sol;strong>  and add below layout code&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;tab&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><strong>&lt&semi;android&period;support&period;design&period;widget&period;CoordinatorLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;design&period;widget&period;AppBarLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;theme&equals;"&commat;style&sol;ThemeOverlay&period;AppCompat&period;Dark&period;ActionBar"&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;v7&period;widget&period;Toolbar&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;toolbar"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"&quest;attr&sol;actionBarSize"&NewLine; android&colon;background&equals;"&quest;attr&sol;colorPrimary"&NewLine; app&colon;layout&lowbar;scrollFlags&equals;"scroll&vert;enterAlways"&NewLine; app&colon;popupTheme&equals;"&commat;style&sol;ThemeOverlay&period;AppCompat&period;Light" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;design&period;widget&period;TabLayout&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tabs"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; app&colon;tabGravity&equals;"fill"&NewLine; app&colon;tabMode&equals;"fixed" &sol;&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;&sol;android&period;support&period;design&period;widget&period;AppBarLayout&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;v4&period;view&period;ViewPager&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;viewpager"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; app&colon;layout&lowbar;behavior&equals;"&commat;string&sol;appbar&lowbar;scrolling&lowbar;view&lowbar;behavior" &sol;&gt&semi;&NewLine;&lt&semi;&sol;android&period;support&period;design&period;widget&period;CoordinatorLayout&gt&semi;&NewLine;&NewLine;&NewLine;<&sol;strong><&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>xml attribute &colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><em><strong>app&colon;tabMode<&sol;strong><&sol;em> <&sol;span>– Defines the mode of the tab layout&period; In our case the value should be <span style&equals;"color&colon; &num;008000&semi;">&OpenCurlyDoubleQuote;<strong>fixed<&sol;strong>”<&sol;span><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Fixed Tabs<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Fixed tabs<&sol;span><&sol;strong> should be used when you have a limited number of tabs&period; These tabs are <strong><span style&equals;"color&colon; &num;008000&semi;">fixed<&sol;span><&sol;strong> in position&period;<&sol;p>&NewLine;<p><img class&equals;"size-medium wp-image-234 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;Screenshot&lowbar;2018-03-15-22-31-161-300x180&period;png" alt&equals;"" width&equals;"300" height&equals;"180" &sol;><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Scrollable Tabs<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">scrollable tabs<&sol;span><&sol;strong> should be used when you have many number of tabs where there is insufficient space on the screen to fit all of them&period; To make the tabs <strong><span style&equals;"color&colon; &num;008000&semi;">scrollable<&sol;span><&sol;strong>&comma; set <span style&equals;"color&colon; &num;008000&semi;"><strong>app&colon;tabMode&equals;”scrollable”<&sol;strong><&sol;span> to TabLayout&period;<&sol;p>&NewLine;<p><img class&equals;"size-medium wp-image-236 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;Screenshot&lowbar;2018-03-15-22-42-071-300x180&period;png" alt&equals;"" width&equals;"300" height&equals;"180" &sol;><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Center Aligned Tabs<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>If you want to keep your tabs<span style&equals;"color&colon; &num;008000&semi;"><strong> horizontally centered<&sol;strong><&sol;span>&comma; assign <span style&equals;"color&colon; &num;008000&semi;"><strong>app&colon;tabGravity&equals;”center”<&sol;strong><&sol;span> to TabLayout&period;<&sol;p>&NewLine;<p><img class&equals;"size-medium wp-image-235 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;Screenshot&lowbar;2018-03-15-22-43-061-300x180&period;png" alt&equals;"" width&equals;"300" height&equals;"180" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Full Width Tabs<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>If you want the tabs to be occupied the <span style&equals;"color&colon; &num;008000&semi;"><strong>fullwidth<&sol;strong><&sol;span> of the screen&comma; you need to assign <span style&equals;"color&colon; &num;008000&semi;"><strong>app&colon;tabGravity&equals;”fill”<&sol;strong><&sol;span> to our TabLayout&period;<&sol;p>&NewLine;<p><img class&equals;"size-medium wp-image-234 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;Screenshot&lowbar;2018-03-15-22-31-161-300x180&period;png" alt&equals;"" width&equals;"300" height&equals;"180" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>8&period;Open <span style&equals;"color&colon; &num;008000&semi;"><strong>TabsActivity&period;java<&sol;strong><&sol;span> and do the below changes&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>setupWithViewPager&lpar;&rpar;<&sol;strong><&sol;span> – Assigns the ViewPager to TabLayout&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>setupViewPager&lpar;&rpar;<&sol;strong><&sol;span> – Defines the number of tabs by setting appropriate fragment and tab name&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>ViewPagerAdapter<&sol;strong><&sol;span> – Custom adapter class provides fragments required for the view pager&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>TabsActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><strong>package com&period;example&period;lenovo&period;tablayout&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;design&period;widget&period;TabLayout&semi;&NewLine;import android&period;support&period;v4&period;app&period;Fragment&semi;&NewLine;import android&period;support&period;v4&period;app&period;FragmentManager&semi;&NewLine;import android&period;support&period;v4&period;app&period;FragmentPagerAdapter&semi;&NewLine;import android&period;support&period;v4&period;view&period;ViewPager&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;support&period;v7&period;widget&period;Toolbar&semi;&NewLine;&NewLine;import com&period;example&period;lenovo&period;tablayout&period;fragment&period;MoviesFragment&semi;&NewLine;import com&period;example&period;lenovo&period;tablayout&period;fragment&period;EventsFragment&semi;&NewLine;import com&period;example&period;lenovo&period;tablayout&period;fragment&period;PlaysFragment&semi;&NewLine;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;&NewLine;public class TabsActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; private Toolbar toolbar&semi;&NewLine; private TabLayout tabLayout&semi;&NewLine; private ViewPager viewPager&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;tabs&rpar;&semi;&NewLine;&NewLine; toolbar &equals; &lpar;Toolbar&rpar; findViewById&lpar;R&period;id&period;toolbar&rpar;&semi;&NewLine; setSupportActionBar&lpar;toolbar&rpar;&semi;&NewLine;&NewLine; getSupportActionBar&lpar;&rpar;&semi;&NewLine;&NewLine; viewPager &equals; &lpar;ViewPager&rpar; findViewById&lpar;R&period;id&period;viewpager&rpar;&semi;&NewLine; setupViewPager&lpar;viewPager&rpar;&semi;&NewLine;&NewLine; tabLayout &equals; &lpar;TabLayout&rpar; findViewById&lpar;R&period;id&period;tabs&rpar;&semi;&NewLine; tabLayout&period;setupWithViewPager&lpar;viewPager&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private void setupViewPager&lpar;ViewPager viewPager&rpar; &lbrace;&NewLine; ViewPagerAdapter adapter &equals; new ViewPagerAdapter&lpar;getSupportFragmentManager&lpar;&rpar;&rpar;&semi;&NewLine; adapter&period;addFragment&lpar;new MoviesFragment&lpar;&rpar;&comma; "MOVIES"&rpar;&semi;&NewLine; adapter&period;addFragment&lpar;new PlaysFragment&lpar;&rpar;&comma; "PLAYS"&rpar;&semi;&NewLine; adapter&period;addFragment&lpar;new EventsFragment&lpar;&rpar;&comma; "EVENTS"&rpar;&semi;&NewLine; viewPager&period;setAdapter&lpar;adapter&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; class ViewPagerAdapter extends FragmentPagerAdapter &lbrace;&NewLine; private final List&lt&semi;Fragment&gt&semi; mFragmentList &equals; new ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi;&NewLine; private final List&lt&semi;String&gt&semi; mFragmentTitleList &equals; new ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi;&NewLine;&NewLine; public ViewPagerAdapter&lpar;FragmentManager manager&rpar; &lbrace;&NewLine; super&lpar;manager&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public Fragment getItem&lpar;int position&rpar; &lbrace;&NewLine; return mFragmentList&period;get&lpar;position&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public int getCount&lpar;&rpar; &lbrace;&NewLine; return mFragmentList&period;size&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void addFragment&lpar;Fragment fragment&comma; String title&rpar; &lbrace;&NewLine; mFragmentList&period;add&lpar;fragment&rpar;&semi;&NewLine; mFragmentTitleList&period;add&lpar;title&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public CharSequence getPageTitle&lpar;int position&rpar; &lbrace;&NewLine; return mFragmentTitleList&period;get&lpar;position&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;strong><&sol;pre>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Now run the app&period; You should able to see the tabs displayed with swipe functionality between the tabs&period;<&sol;span><&sol;strong><&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-229" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;Screenshot&lowbar;2018-03-15-18-02-02-180x300&period;png" alt&equals;"" width&equals;"215" height&equals;"358" &sol;>  <img class&equals;"alignnone wp-image-230" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;Screenshot&lowbar;2018-03-15-18-02-12-180x300&period;png" alt&equals;"" width&equals;"214" height&equals;"357" &sol;>  <img class&equals;"alignnone wp-image-231" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;Screenshot&lowbar;2018-03-15-18-02-23-180x300&period;png" alt&equals;"" width&equals;"214" height&equals;"356" &sol;>&NewLine;

Exit mobile version