Material Tabs Revisited
This isn’t a detailed tutorial on how to implement material tabs but just a quick revisit on’em. I assume you’ve created your project and everything is well set. Head over Materialpallte for awesome color choices and download an xml type of colors.
With Toolbar aboard in replacement of ActionBar, lets start by adding it to our new layout. roughly that would translate to something like this.
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"
android:minHeight="?attr/actionBarSize"
android:paddingTop="@dimen/appbar_top_padding"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
Then came material tabs
For my lazy fellas we gotta head over to our browsers for two major classes, Mr. SlidingTabLayout.java
and Mrs.SlidingTabStrip.java
.
I like to get mine from Google IO app Here but you can also get them elsewhere. There is no rule that you should get them from somewhere but my laziness tells me so.
Their names are self explanatory so lets see what happens under the hood.
With no error nearby, head over to xml
and add some lines
first, add SlidingTabLayout just below Toolbar
. snippet for SlidingTablayout goes like this
<android.aaron.com.material.tabs.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary>
</android.aaron.com.material.tabs.SlidingTabLayout>
but watch out for the package name in <code>android.aaron.com.material.tabs.SlidingTabLayout</code>. Make sure you replace it with your package.
Now add <code>Viewpager</code> below them. Something like
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true">
</android.support.v4.view.ViewPager></code>
With xml ready to rock and roll, head back to java
Start by doing the findViewById()
thing or use Butterknife if you are farmiliar with it (You should actually use the later...). So far so good? Thats what I thought.
Them Fragments
I was thinking of using one fragment but then why not create seperate ones for diffrent purpose. Lets say we wanna create something like this...
Create three fragments. A simple fragment class looks like this
public class PageTwo extends Fragment {
public PageTwo() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_page_two, container, false);
return view;
}
}
}
Mr Adapter, where you at?
Create a class, name it something like MaterialTabsAdapter or something not weird. With simplicity kept constant, extend FragmentPagerAdapter
in the getItem(int position)
method, you can get fragments by,
@Override
public Fragment getItem(int position) {
Fragment fragment;
switch (position) {
case 0:
fragment = new PageOne();
return fragment;
case 1:
fragment = new PageThree();
return fragment;
case 2:
fragment = new PageTwo();
return fragment;
default:
fragment = new PageOne();
return fragment;
}
}
This means that when we are in a particular page we load a particular fragment
@Override
public int getCount() {
//The number of fragments we got
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
//Sample titles for our tabs
if (position == 0) {
return "Movies";
} else if (position == 1) {
return "Series";
} else if (position == 2) {
return "Films";
}
return "";
}
So far we've done nearly everything.
Finally...
Lets now make this class to run.
Open
MainActivity.java
MaterialTabsAdapter myFragmentAdapter = new MaterialTabsAdapter(getSupportFragmentManager());
viewPager.setAdapter(myFragmentAdapter);
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setViewPager(viewPager);
This was my first post and it's probably terrible. Cheers!