{"id":1781,"date":"2014-10-12T23:24:36","date_gmt":"2014-10-12T17:54:36","guid":{"rendered":"http:\/\/codetheory.in\/?p=1781"},"modified":"2014-10-13T13:16:15","modified_gmt":"2014-10-13T07:46:15","slug":"android-pagertabstrip-pagertitlestrip-viewpager","status":"publish","type":"post","link":"https:\/\/codetheory.in\/android-pagertabstrip-pagertitlestrip-viewpager\/","title":{"rendered":"Android PagerTabStrip and PagerTitleStrip with ViewPager"},"content":{"rendered":"
Note:<\/strong> This post is sort of a continuation of my previous post<\/a> on When working with <\/p>\n Both of them are supposed to be used as a child view of the Similarly, this is how the layout code for a Not much of a difference in the code between both the versions. Only the tag name and ID got changed.<\/p>\n Now when you test this code in your emulator or a physical android device, then you’ll notice a blue strip at the top of the view pager. You’ll be able to navigate through the pager screens but there won’t be any text in the pager strip unless they’re specified in the The method should return For the sake of quickly testing `PagerTabStrip` I’ve put together some code. Here’s the Activity bit:<\/p>\n This is how the Finally here’s the Go ahead and test your app now! Play with the view pager and its top-aligned pager strip. You can also customize the Although both of them are quite similar there are certain differences which makes On going through the public methods for both in the docs, you’ll notice I came across this plugin called ViewPagerIndicator<\/a> that allows you to customize your pager strip in so many different ways. You can even replicate the google play scrollable tabs with this plugin.<\/p>\n Hope that helps!<\/p>\n","protected":false},"excerpt":{"rendered":" Note: This post is sort of a continuation of my previous post on ViewPager and its related pager adapters with tabs. When working with ViewPager we most likely include action bar tabs and integrate them with their corresponding screens in the ViewPager. Action bar tabs works great but just incase if you prefer scrollable tabs … Continue reading “Android PagerTabStrip and PagerTitleStrip with ViewPager”<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[105,89],"_links":{"self":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1781"}],"collection":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/comments?post=1781"}],"version-history":[{"count":4,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1781\/revisions"}],"predecessor-version":[{"id":1785,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1781\/revisions\/1785"}],"wp:attachment":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/media?parent=1781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/categories?post=1781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/tags?post=1781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}ViewPager<\/code> and its related pager adapters with tabs.<\/p>\n
ViewPager<\/code><\/a> we most likely include action bar tabs and integrate them with their corresponding screens in the
ViewPager<\/code>. Action bar tabs works great but just incase if you prefer scrollable tabs<\/a> and want to use them over action bar tabs then there are two ways to achieve that:<\/p>\n
\n
PagerTabStrip<\/code><\/a><\/li>\n
PagerTitleStrip<\/code><\/a><\/li>\n<\/ul>\n
ViewPager<\/code> widget in the XML layout resource. So here’s how a
ViewPager<\/code> with a
PagerTabStrip<\/code> in it would look like in the layout file:<\/p>\n
\r\n<android.support.v4.view.ViewPager xmlns:android="http:\/\/schemas.android.com\/apk\/res\/android"\r\n android:id="@+id\/pager"\r\n android:layout_width="match_parent"\r\n android:layout_height="match_parent">\r\n\r\n <android.support.v4.view.PagerTabStrip\r\n android:id="@+id\/pager_tab_strip"\r\n android:layout_width="match_parent"\r\n android:layout_height="wrap_content"\r\n android:layout_gravity="top"\r\n android:background="#33b5e5"\r\n android:textColor="#fff"\r\n android:paddingTop="5dp"\r\n android:paddingBottom="5dp" \/>\r\n\r\n\r\n<\/android.support.v4.view.ViewPager>\r\n<\/pre>\n
ViewPager<\/code> with a top-aligned
PagerTitleStrip<\/code> would be like:<\/p>\n
\r\n<android.support.v4.view.ViewPager xmlns:android="http:\/\/schemas.android.com\/apk\/res\/android"\r\n android:id="@+id\/pager"\r\n android:layout_width="match_parent"\r\n android:layout_height="match_parent">\r\n\r\n <android.support.v4.view.PagerTitleStrip\r\n android:id="@+id\/pager_title_strip"\r\n android:layout_width="match_parent"\r\n android:layout_height="wrap_content"\r\n android:layout_gravity="top"\r\n android:background="#33b5e5"\r\n android:textColor="#fff"\r\n android:paddingTop="5dp"\r\n android:paddingBottom="5dp" \/>\r\n\r\n\r\n<\/android.support.v4.view.ViewPager>\r\n<\/pre>\n
getPageTitle()<\/code><\/a> method of the pager adapter implementation which should return the title for specific pages. The method implementation should look something like this:<\/p>\n
\r\n@Override\r\npublic CharSequence getPageTitle(int position) {\r\n switch (position) {\r\n case 0:\r\n return "Tab One";\r\n case 1:\r\n return "Tab Two";\r\n case 2:\r\n return "Tab Three";\r\n }\r\n\r\n return null;\r\n}\r\n<\/pre>\n
null<\/code> which would indicate no title for that particular page. Although I used
switch<\/code> block you could also write the same code in another way by using arrays and returning the corresponding title by using the
position<\/code> argument as the array index.<\/p>\n
Code Sample<\/h2>\n
\r\npublic class MainActivity extends Activity {\r\n\r\n CustomPagerAdapter mCustomPagerAdapter;\r\n ViewPager mViewPager;\r\n\r\n @Override\r\n protected void onCreate(Bundle savedInstanceState) {\r\n super.onCreate(savedInstanceState);\r\n setContentView(R.layout.activity_main);\r\n\r\n mCustomPagerAdapter = new CustomPagerAdapter(getFragmentManager(), this);\r\n\r\n mViewPager = (ViewPager) findViewById(R.id.pager);\r\n mViewPager.setAdapter(mCustomPagerAdapter);\r\n }\r\n\r\n class CustomPagerAdapter extends FragmentPagerAdapter {\r\n\r\n Context mContext;\r\n\r\n public CustomPagerAdapter(FragmentManager fm, Context context) {\r\n super(fm);\r\n mContext = context;\r\n }\r\n\r\n @Override\r\n public Fragment getItem(int position) {\r\n\r\n \/\/ Create fragment object\r\n Fragment fragment = new DemoFragment();\r\n\r\n \/\/ Attach some data to the fragment\r\n \/\/ that we'll use to populate our fragment layouts\r\n Bundle args = new Bundle();\r\n args.putInt("page_position", position + 1);\r\n\r\n \/\/ Set the arguments on the fragment\r\n \/\/ that will be fetched in the\r\n \/\/ DemoFragment@onCreateView\r\n fragment.setArguments(args);\r\n\r\n return fragment;\r\n }\r\n\r\n @Override\r\n public int getCount() {\r\n return 3;\r\n }\r\n\r\n @Override\r\n public CharSequence getPageTitle(int position) {\r\n return "Page " + (position + 1);\r\n }\r\n }\r\n\r\n class DemoFragment extends Fragment {\r\n @Override\r\n public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {\r\n \/\/ Inflate the layout resource that'll be returned\r\n View rootView = inflater.inflate(R.layout.fragment_demo, container, false);\r\n\r\n \/\/ Get the arguments that was supplied when\r\n \/\/ the fragment was instantiated in the\r\n \/\/ CustomPagerAdapter\r\n Bundle args = getArguments();\r\n ((TextView) rootView.findViewById(R.id.textView)).setText("Page " + args.getInt("page_position"));\r\n\r\n return rootView;\r\n }\r\n }\r\n\r\n @Override\r\n public boolean onCreateOptionsMenu(Menu menu) {\r\n \/\/ Inflate the menu; this adds items to the action bar if it is present.\r\n getMenuInflater().inflate(R.menu.gesture, menu);\r\n return true;\r\n }\r\n\r\n @Override\r\n public boolean onOptionsItemSelected(MenuItem item) {\r\n \/\/ Handle action bar item clicks here. The action bar will\r\n \/\/ automatically handle clicks on the Home\/Up button, so long\r\n \/\/ as you specify a parent activity in AndroidManifest.xml.\r\n int id = item.getItemId();\r\n if (id == R.id.action_settings) {\r\n return true;\r\n }\r\n return super.onOptionsItemSelected(item);\r\n }\r\n}\r\n<\/pre>\n
res\/layout\/activity_main.xml<\/code> should be like:<\/p>\n
\r\n<RelativeLayout xmlns:android="http:\/\/schemas.android.com\/apk\/res\/android"\r\n xmlns:tools="http:\/\/schemas.android.com\/tools"\r\n android:layout_width="match_parent"\r\n android:layout_height="match_parent"\r\n android:paddingLeft="@dimen\/activity_horizontal_margin"\r\n android:paddingRight="@dimen\/activity_horizontal_margin"\r\n android:paddingTop="@dimen\/activity_vertical_margin"\r\n android:paddingBottom="@dimen\/activity_vertical_margin"\r\n android:id="@+id\/relativeLayout">\r\n\r\n <android.support.v4.view.ViewPager xmlns:android="http:\/\/schemas.android.com\/apk\/res\/android"\r\n android:id="@+id\/pager"\r\n android:layout_width="match_parent"\r\n android:layout_height="match_parent">\r\n\r\n <android.support.v4.view.PagerTabStrip\r\n android:id="@+id\/pager_title_strip"\r\n android:layout_width="match_parent"\r\n android:layout_height="wrap_content"\r\n android:layout_gravity="top"\r\n android:background="#33b5e5"\r\n android:textColor="#fff"\r\n android:paddingTop="5dp"\r\n android:paddingBottom="5dp" \/>\r\n\r\n <\/android.support.v4.view.ViewPager>\r\n\r\n<\/RelativeLayout>\r\n<\/pre>\n
res\/layout\/fragment_demo.xml<\/code>:<\/p>\n
\r\n<?xml version="1.0" encoding="utf-8"?>\r\n\r\n<LinearLayout xmlns:android="http:\/\/schemas.android.com\/apk\/res\/android"\r\n android:orientation="vertical" android:layout_width="match_parent"\r\n android:layout_height="match_parent">\r\n\r\n <TextView\r\n android:layout_width="wrap_content"\r\n android:layout_height="wrap_content"\r\n android:text="New Text"\r\n android:id="@+id\/textView" \/>\r\n<\/LinearLayout>\r\n<\/pre>\n
PagerTabStrip<\/code> programatically. Feel free to go through the public methods here<\/a>.<\/p>\n
PagerTabStrip vs PagerTitleStrip – the Difference<\/h2>\n
PagerTabStrip<\/code> superior compared to the other one and hence most likely you’ll always want to use it over the other.<\/p>\n
PagerTitleStrip<\/code> only indicates the section we’re in through the title names. On the contrary,
PagerTabStrip<\/code> not only indicates the section name but also has the tab indicator (bottom border) that depicts the active tab. Also
PagerTabStrip<\/code> is interactive which means you can click on the tabs to switch among screens while you cannot do that with
PagerTitleStrip<\/code> (major limitation IMHO).<\/p>\n
PagerTabStrip<\/code><\/a> has marginally more methods or options to customize compared to
PagerTitleStrip<\/code><\/a>.<\/p>\n